剑道ui MVC网格弹出编辑器模板DropDownListFor不工作

时间:2015-10-06 22:20:26

标签: asp.net-mvc razor kendo-ui kendo-grid kendo-asp.net-mvc

我正在使用带有Razor视图的ASP.MVC的Kendo UI,并且在网格编辑弹出模式下,EditorTemplate不会在DropDownListFor中显示Grid的值。我错过了什么?

查看:

@(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; }    
}

1 个答案:

答案 0 :(得分:0)

更改_CustomerEditor模板模型字段
$(this).text()

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

正如评论所说,&#34;此名称必须与主网格页面上的标题相同&#34;

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