Grid ForeignKey列未选择剑道网格中的值
我有一个带有一些列的网格,其中一列是foreignKey列 我想在组合框中显示该列的所有可能值Grid ForeignKey列未选择剑道网格中的值,grid,kendo-ui,Grid,Kendo Ui,我有一个带有一些列的网格,其中一列是foreignKey列 我想在组合框中显示该列的所有可能值ViewData[“States”]是一个IList,其中State有一个Id字段和一个Name字段 为了实现这一点,我修改了模板“GridForeignKey.cshtml”,如下所示: @model object @( Html.Kendo().ComboBoxFor(m => m) .BindTo((SelectList)ViewData[ViewData.Tem
ViewData[“States”]
是一个IList
,其中State有一个Id
字段和一个Name
字段
为了实现这一点,我修改了模板“GridForeignKey.cshtml”,如下所示:
@model object
@(
Html.Kendo().ComboBoxFor(m => m)
.BindTo((SelectList)ViewData[ViewData.TemplateInfo.GetFullHtmlFieldName("") +
"_Data"]).Filter(FilterType.Contains).Placeholder("Select...")
)
我的观点是这样的:
<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>
@{
ViewBag.Title=“主页”;
Layout=“~/Views/Shared/_Layout.cshtml”;
}
@(
Html.Kendo().Grid()
.名称(“网格”)
.列(列=>{
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(“整数”);
columns.Command(Command=>{Command.Edit();Command.Destroy();});//编辑和删除按钮
})
.ToolBar(ToolBar=>ToolBar.Create())//添加按钮
.Editable(Editable=>Editable.Mode(GridEditMode.InLine))//内联编辑
.Pageable()
.Sortable()
.Scrollable()
.可过滤()
.DataSource(DataSource=>DataSource
.Ajax()
.Events(Events=>Events.Error(“错误处理程序”))
.Model(Model=>{
Id(c=>c.CustomerId);
model.Field(c=>c.CustomerId).可编辑(false);
model.Field(c=>c.Zip).DefaultValue(4444);
}
)//客户身份证
.Create(update=>update.Action(“编辑行\创建”、“客户”))
.Read(Read=>Read.Action(“editingline\u Read”,“Customer”))
.Update(Update=>Update.Action(“编辑行\更新”、“客户”))
.Destroy(update=>update.Action(“编辑行\销毁”、“客户”))
)
)
函数错误\u处理程序(e){
如果(如错误){
var message=“错误:\n”;
$.e`在此处输入代码`ach(e.错误、函数(键、值){
如果(值中的“错误”){
$.each(value.errors,函数(){
消息+=此+“\n”;
});
}
});
警报(消息);//显示错误
}
}
现在我的问题是:
State
,其中包含Id
和Name
属性),但不知怎的,kendo不让我像p=>p.State.Id
那样绑定它。这就是我将模型展平的原因,现在我使用字段StateId
。甚至可以使用这样的级联复杂类型吗你所拥有的不会起作用。您需要将EditorTemplate传递给EditorViewData中的列表,并告诉它要使用哪个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"])
)
这可能并不完全正确,因为我只是一时冲动。但它至少应该让您朝着正确的方向前进。要实现您想要的,您需要在网格中定义编辑事件后实现一些客户端脚本
.Events(events => events.Edit("onEdit"))
if your grid called **myGrid** then your script will be in this way
<script>
$(document).ready(function (e) {
var innerContent = $(".k-grid-delete").html().replace("Delete", "");
$(".k-grid-delete").html(innerContent);
});
function onEdit(e) {
$("#**myGrid** tbody [data-role=dropdownlist]").each(function () {
var ddl = $(this).data("kendoDropDownList");
if (ddl) {
ddl.options.optionLabel = "Select";
ddl.refresh();
ddl.value("");
}
})
}
.Events(Events=>Events.Edit(“onEdit”))
如果您的网格名为**myGrid**,那么您的脚本将采用这种方式
$(文档).ready(函数(e){
var innerContent=$(“.k-grid-delete”).html().replace(“delete”,”);
$(“.k-grid-delete”).html(innerContent);
});
功能OneEdit(e){
$(“#**myGrid**tbody[data role=dropdownlist]”)。每个(函数(){
var ddl=$(this).data(“kendoDropDownList”);
中频(ddl){
ddl.options.optionLabel=“选择”;
ddl.refresh();
ddl.值(“”);
}
})
}
我有点困惑:似乎您要传递viewdata两次?!当我选择foreignkey colum时,GridForeignKey模板被隐式调用。不管怎样,我尝试了你的建议,结果和我的完全一样。(问题依然存在)。不过还是谢谢你的回复!