2015-10-06 123 views
1

我在使用Razor视图和网格编辑弹出模式下使用Kendo UI为ASP.MVC,EditorTemplate在DropDownListFor中不显示Grid中的值。我错过了什么?Kendo ui MVC Grid弹出式编辑器模板DropDownListFor不工作

检视:

@(Html.Kendo().Grid<CustomerLeadViewModel>() 
    .Name("grid")   
    .Columns(columns => 
    { 
     columns.Bound(c => c.CustomerName).Width(200); 
     columns.Bound(c => c.CustomerGroup);   
     columns.Bound(c => c.CustomerActivityField);    
     columns.Bound(c => c.AgentName);   

     columns.Command(command => 
     { 
      command.Edit().Text("Edit"); 
      command.Destroy().Text("Delete");     
     }).Width(250); 
    }) 
    .HtmlAttributes(new { style = "height: 480px;" }) 
      .Editable(editable => editable.Mode(GridEditMode.PopUp)).TemplateName("_CustomerEdit"))        
      .DataSource(dataSource => dataSource 
      .Ajax()     
      .Model(model => 
      { 
        model.Id(c => c.Id);                      
      })     
      .Read(read => read.Action("Customers_Read", "CustomerLead", new { model = Model })) 
      .Update(update => update.Action("Customers_Update", "CustomerLead")) 
      .Destroy(delete => delete.Action("Customers_Destroy", "CustomerLead")) 
    ) 
) 

和EditorTemplate/_CustomerEdit:

@model CustomerLeadViewModel 
@Html.HiddenFor(model => model.Id) 
@Html.Kendo().TextBoxFor(model => model.CustomerName) 
@(
Html.Kendo().DropDownListFor(model=>model.CustomerGroupId) 
    .Name("CustomerGroup") //This name has to be the same as the Title on the main grid page 
    .DataTextField("GroupName") 
    .DataValueField("Id") 
     .DataSource(source => 
     { 
      source.Read(read => { read.Action("GetCustomerGroups", "CustomerLead"); }); 
     }) 

    .OptionLabel("Select a group ...") 
    ) 

控制器:

public ActionResult Customers_Read([DataSourceRequest]DataSourceRequest request, CustomerLeadSearchViewModel model) 
    { 
     var customers = customerLeadRepository.Get(includeProperties: "CustomerSubGroup.CustomerGroup, CustomerActivityField"); 

     if (model.CustomerName != null) 
     { 
      customers = customers.Where(c => c.CustomerName.Contains(model.CustomerName)); 
     } 

     var customersViewModel = customers.Select(customer => new CustomerLeadViewModel 
     { 
      Id = customer.Id, 
      CustomerGroupId = customer.CustomerGroupId, 
      CustomerGroup = customer.CustomerSubGroup.CustomerGroup.GroupName,     
      CustomerName = customer.CustomerName, 
      CustomerActivityField = customer.CustomerActivityField.ActivityName, 
      AgentName = customer.AgentName, 
      Phone1 = customer.Phone1, 
      Mobile = customer.Mobile 
     }); 
     return Json(customersViewModel.ToDataSourceResult(request)); 
    } 
    public JsonResult GetCustomerGroups() 
    {   
     var customerGroups = customerGroupRepository.GetAll(); 
     return Json(customerGroups.Select(c => new { Id = c.Id, GroupName =  c.GroupName }), JsonRequestBehavior.AllowGet); 
    } 

型号:

public class CustomerLeadViewModel 
{ 
    [ScaffoldColumn(false)] 
    public int Id { get; set; } 
    public string CustomerName { get; set; } 
    public string AgentName { get; set; } 
    public string Post { get; set; } 
    public string CustomerActivityField { get; set; } 
    public int CustomerGroupId { get; set; } 
    public string CustomerGroup { get; set; }  
} 

回答

0

@(Html.Kendo().DropDownListFor(model=>model.CustomerGroupId) 

@(Html.Kendo().DropDownListFor(model=>model.CustomerGroup) 

正如评论说,改变你的_CustomerEditor样板示范田“这个名字必须是一样的格主网页上的标题是”

DropDownListFor绑定的字段需要相同。或者,根据您的模型,将它们全部更改为CustomerGroupId。无论哪种方式,他们都需要匹配。

相关问题