c# - 当绑定(bind)到可为空字段时,Kendo 网格中的级联下拉列表返回 null

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

我有一个剑道网格,其中包含两组级联下拉列表:一组用于位置级别 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 而不将这些字段更改为不可为空?

最佳答案

经过一番搜索,我在 http://www.sitereq.com/post/kendo-mvc-dropdown-lists-inside-inline-kendo-mvc-grids 找到了答案。

Take care of the nullable model properties
This is an important note to take care of. In case your model consists of nullable properties of integer, floats or even byte types, the Kendo grid will not be able to update the model properties to their values on create or edit events. It's a known bug in Kendo grids having Kendo drop down lists in their editor template. So in case the CompanyId is Nullable instead of int then to work around this you have to add the "save" event to the grid like in the following listing

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

where EmployeesGrid_Save is the javascript handler that will handle the grid save event. The following listing describe how the save handler will help the grid to save the values of the drop down lists to their corresponding nullable properties.

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

我实现了这个,它有效!

关于c# - 当绑定(bind)到可为空字段时,Kendo 网格中的级联下拉列表返回 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29144057/

相关文章:

asp.net-mvc - MVC中的授权属性顺序、优先级和功能问题

datetime - Kendo UI 网格格式日期列

javascript - 将 Kendo UI 网格高度设置为包装器的 100%

c# - 安全句柄和句柄引用

asp.net-mvc - 如何将 IIS 中的默认页面设置为 Controller ?

c# - xsd.exe 生成类的可空值

asp.net-mvc - ASP.NET MVC : RenderAction for action writing image throws exception

javascript - 设置 Kendo 网格过滤器单元格值

c# - 将通用图像加载器绑定(bind)到 Xamarin 解决方案

c# - 强制 .NET GC 压缩或整理第 2 代堆?