gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 21:59:38 26 4
gpt4 key购买 nike

我有一个剑道网格,其中包含两组级联下拉列表:一组用于位置级别 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com