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

时间:2015-03-19 12:02:05

标签: c# asp.net-mvc kendo-grid

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

我使用内联编辑,所有组合都正确填充。但是,当我保存时,可以为空的组合框的选定值不包含在帖子中,并且ActionResult会为模型的这两个字段接收空值。

网格:

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

    }

如何将类别2级和3级值添加到ActionResult WITHOUT 将这些字段更改为不可空?

1 个答案:

答案 0 :(得分:3)

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

找到答案
  

照顾可以为空的模型属性
    这是一个需要注意的重要注意事项。如果你的模型包含可空的   Kendo网格将是整数,浮点数或甚至字节类型的属性   无法在创建时将模型属性更新为其值   或编辑事件。这是Kendo网格中已知的一个让Kendo下降的错误   列表在他们的编辑器模板中所以,如果是CompanyId   Nullable而不是int然后解决这个问题你必须添加   “保存”事件到网格,如下面的列表

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

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

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

我实现了这个,它有效!