我有一个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?

最佳答案

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


  注意可为空的模型属性
    这是要注意的重要注意事项。如果您的模型包含可为空的
  整数,浮点数甚至字节类型的属性,Kendo网格将
  创建时无法将模型属性更新为其值
  或编辑事件。这是Kendo网格中一个已知错误,导致Kendo下拉
  在其编辑器模板中列出。因此,如果CompanyId为
  可为空而不是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);
    }



我实现了它,并且有效!

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

10-17 01:07