gpt4 book ai didi

grid - ForeignKey 列不选择 Kendo-Grid 中的值

转载 作者:行者123 更新时间:2023-12-04 20:13:14 24 4
gpt4 key购买 nike

我有一个包含一些列的网格,其中一个列是 foreignKey 列。

我想在组合框中显示该列的所有可能值。 ViewData["States"]IList<State>其中 State 有 Id字段和 Name field 。

为此,我修改了模板“GridForeignKey.cshtml”,如下所示:

@model object

@(
Html.Kendo().ComboBoxFor(m => m)
.BindTo((SelectList)ViewData[ViewData.TemplateInfo.GetFullHtmlFieldName("") +
"_Data"]).Filter(FilterType.Contains).Placeholder("Select...")
)

我的 View 如下所示:
<div class="contentwrapper">
@{
ViewBag.Title = "Home Page";
Layout = "~/Views/Shared/_Layout.cshtml";
}

@(
Html.Kendo().Grid<CustomerModel>()
.Name("Grid")
.Columns(columns => {
columns.Bound(p => p.Name);

columns.ForeignKey(p => p.StateId, (IEnumerable)ViewData["States"], "Id", "Name");

columns.Bound(p => p.StreetAddress).Width(140);
columns.Bound(p => p.Zip).EditorTemplateName("Integer");
columns.Command(command => { command.Edit(); command.Destroy(); });//edit and delete buttons
})
.ToolBar(toolbar => toolbar.Create())//add button
.Editable(editable => editable.Mode(GridEditMode.InLine))//inline edit
.Pageable()
.Sortable()
.Scrollable()
.Filterable()
.DataSource(dataSource => dataSource
.Ajax()
.Events(events => events.Error("error_handler"))
.Model(model => {
model.Id(c => c.CustomerId);
model.Field(c => c.CustomerId).Editable(false);
model.Field(c => c.Zip).DefaultValue(4444);
}
)//id of customer
.Create(update => update.Action("EditingInline_Create", "Customer"))
.Read(read => read.Action("EditingInline_Read", "Customer"))
.Update(update => update.Action("EditingInline_Update", "Customer"))
.Destroy(update => update.Action("EditingInline_Destroy", "Customer"))
)
)
</div>
<script type="text/javascript">
function error_handler(e) {
if (e.errors) {
var message = "Errors:\n";
$.e`enter code here`ach(e.errors, function (key, value) {
if ('errors' in value) {
$.each(value.errors, function () {
message += this + "\n";
});
}
});
alert(message);//show error
}
}
</script>

现在我的问题:
  • 我的表不显示“状态”的选定值。
  • 当我编辑一行时,组合框会显示并包含所有所需的值,但未选择该值。相反,它总是显示占位符文本。
  • 在我将一个复杂对象绑定(bind)到我的网格之前,该对象有一个本身就是复杂类型的字段(State,其中包含 IdName 属性),但不知何故,剑道不允许我像 p => p.State.Id 那样绑定(bind)它.这就是我扁平化模型的原因,现在我使用字段 StateId反而。甚至可以使用这样的级联复杂类型吗?
  • 最佳答案

    你所拥有的将不起作用。您需要将 EditorViewData 中的列表传递给 EditorTemplate,并告诉它要使用哪个 EditorTemplateName。

    columns.ForeignKey(p => p.StateId, (IEnumerable)ViewData["States"], "Id", "Name")
    .EditorViewData(new { statesList = ViewData["States"] })
    .EditorTemplateName("GridForeignKey");

    和 GridForeignKey.cshtml 一样
    @model int // Assuming Id is an int
    @(Html.Kendo().ComboBoxFor(m => m)
    .Placeholder("Select...")
    .DataValueField("Id")
    .DataTextField("Name")
    .BindTo(ViewData["statesList"])
    )

    这可能不完全正确,因为我只是在脑海中做到了。但它至少应该让你朝着正确的方向前进。

    关于grid - ForeignKey 列不选择 Kendo-Grid 中的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14983517/

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