Kendo 网格中的级联下拉列表 returns 绑定到可为空字段时为 null
Cascading dropdown in Kendo grid returns null when bound to nullable field
我有一个 kendo 网格,其中包含两组级联下拉列表:一组用于位置级别 1 到 3,另一组用于类别级别 1 到 3。在我的模型中,位置都不能为空,而对于类别级别 2 和级别 3 可以为空。
我使用内联编辑,所有组合都正确填充。但是,当我保存时,可空组合框的选定值未包含在 post 中,并且 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);
}
我实现了这个,并且有效!
我有一个 kendo 网格,其中包含两组级联下拉列表:一组用于位置级别 1 到 3,另一组用于类别级别 1 到 3。在我的模型中,位置都不能为空,而对于类别级别 2 和级别 3 可以为空。
我使用内联编辑,所有组合都正确填充。但是,当我保存时,可空组合框的选定值未包含在 post 中,并且 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); }
我实现了这个,并且有效!