Kendo网格中的级联下拉列表在绑定到可为null的字段时返回null



我有一个剑道网格,它包含两组级联下拉列表:一组用于位置级别1到3,另一组用于类别级别1到3。在我的模型中,位置都不可为空,而类别级别2和级别3是可为空的。

我使用内联编辑,并且所有组合框都正确填充。然而,当我保存时,post中不包括可为null的组合框的选定值,ActionResult接收到模型的这两个字段的null值。

网格:

 @(Html.Kendo().Grid<Container>()
              .Name("ContainerGrid")
              .Columns(columns =>
              {
                  columns.Command(command => { command.Edit(); }).Width(150);                 
                  columns.ForeignKey(c => c.LocationLevel1Id, (System.Collections.IEnumerable)ViewData["locationLevel1"], "Id", "Text").EditorTemplateName("LocationLevel1Id");
                  columns.ForeignKey(c => c.LocationLevel2Id, (System.Collections.IEnumerable)ViewData["locationLevel2"], "Id", "Text").EditorTemplateName("LocationLevel2Id");
                  columns.ForeignKey(c => c.LocationLevel3Id, (System.Collections.IEnumerable)ViewData["locationLevel3"], "Id", "Text").EditorTemplateName("LocationLevel3Id");
                  columns.ForeignKey(c => c.CategoryLevel1Id, (System.Collections.IEnumerable)ViewData["catLevel1"], "Id", "Text").EditorTemplateName("CategoryLevel1Id");
                  columns.ForeignKey(c => c.CategoryLevel2Id, (System.Collections.IEnumerable)ViewData["catLevel2"], "Id", "Text").EditorTemplateName("CategoryLevel2Id");
                  columns.ForeignKey(c => c.CategoryLevel3Id, (System.Collections.IEnumerable)ViewData["catLevel3"], "Id", "Text").EditorTemplateName("CategoryLevel3Id");
              })
                 .ColumnMenu()
                .Editable(editable => editable.Mode(GridEditMode.InLine))             
                .Selectable(selectable => selectable.Mode(GridSelectionMode.Single))
                .DataSource(dataSource => dataSource
                    .Ajax()
                    .PageSize(100)
                    .Model(model =>
                    {
                        model.Id(a => a.Id);                      
                        model.Field(a => a.LocationLevel1Id);
                        model.Field(a => a.LocationLevel2Id);
                        model.Field(a => a.LocationLevel3Id);                      
                        model.Field(a => a.CategoryLevel1Id);
                        model.Field(a => a.CategoryLevel2Id);
                        model.Field(a => a.CategoryLevel3Id);
                    })
                    .Read(read => read.Action("Containers_Read", "ContainerAdmin").Data("filterInfo").Type(HttpVerbs.Get))
                    .Create(update => update.Action("Containers_Create", "ContainerAdmin"))
                    .Update(update => update.Action("Containers_Update", "ContainerAdmin"))
                )
        )  

型号:

public class Container
    {
        [Key]
        public Guid Id { get; set; }
        [ForeignKey("LocationLevel1")]
        public Guid LocationLevel1Id { get; set; }
        [JsonProperty(PropertyName = "locationLevel1")]
        public virtual Location LocationLevel1 { get; set; }
        [ForeignKey("LocationLevel2")]
        public Guid LocationLevel2Id { get; set; }
        [JsonProperty(PropertyName = "locationLevel2")]
        public virtual Location LocationLevel2 { get; set; }
        [ForeignKey("LocationLevel3")]
        public Guid LocationLevel3Id { get; set; }
        [JsonProperty(PropertyName = "locationLevel3")]
        public virtual Location LocationLevel3 { get; set; }
        [ForeignKey("CategoryLevel1")]
        public Guid? CategoryLevel1Id { get; set; }
        [JsonProperty(PropertyName = "categoryLevel1")]
        public virtual Category CategoryLevel1 { get; set; }
        [ForeignKey("CategoryLevel2")]
        public Guid? CategoryLevel2Id { get; set; }
        [JsonProperty(PropertyName = "categoryLevel2")]
        public virtual Category CategoryLevel2 { get; set; }
        [ForeignKey("CategoryLevel3")]
        public Guid? CategoryLevel3Id { get; set; }
        [JsonProperty(PropertyName = "categoryLevel3")]
        public virtual Category CategoryLevel3 { get; set; }
    }

如果不将这些字段更改为不可为null,我如何将类别级别2和级别3的值添加到ActionResult?

经过多次搜索,我在http://www.sitereq.com/post/kendo-mvc-dropdown-lists-inside-inline-kendo-mvc-grids

注意可为null的模型属性
这是一个需要注意的重要事项。如果您的模型包含可为null的整数、浮点甚至字节类型的属性,Kendo网格将无法在创建时将模型属性更新为其值或编辑事件。剑道掉落是剑道网格中的一个已知错误列表。因此,如果CompanyId为Nullable而不是int,那么要解决这个问题,你必须添加将"保存"事件保存到网格中,如下面的列表所示

.Events(events =>
    {
        events.Save("EmployeesGrid_Save");
    })

其中EmployeesGrid_Save是将处理网格保存事件。以下列表描述了如何保存处理程序将帮助网格保存下拉列表的值到其相应的可为null的属性。

function EmployeesGrid_Save(e) {
        var companyId = $("#CompanyId").data().kendoDropDownList.value();
        e.model.set("CompanyId", companyId);
    }

我实现了这一点,而且很有效!

最新更新