Kendo下拉框点击行内编辑(Inline Edit)时无法保留值
解决Kendo UI行内编辑Industry列无法保留值的问题
核心问题分析
你的列绑定字段是IndustryName(字符串类型),但下拉编辑器的DataValueField设置为IndustryId(主键字段),两者不匹配导致编辑时无法正确回显原有值,提交也无法完成模型绑定。
具体修复步骤
修正列绑定配置
将列绑定从IndustryName改为IndustryId,同时添加ClientTemplate保证非编辑状态下显示IndustryName文本:columns.Bound(c => c.IndustryId) .Title("Industry") .EditorTemplateName("AppPackIndEditor") .ClientTemplate("#= IndustryName #") .Filterable(ftb => ftb.Extra(false).Operators(Operator => Operator.ForString(str => str.Clear().Contains("Contains")))) .Width(120);调整下拉编辑器设置
移除硬编码的.SelectedIndex(0)(该设置会强制覆盖原有选中值),同时确保编辑器名称与绑定字段一致:@using Kendo.Mvc.UI @(Html.Kendo().DropDownListFor(m => m) .Name("IndustryId") .Filter(FilterType.StartsWith) .DataTextField("IndustryName") .DataValueField("IndustryId") .HtmlAttributes(new { style = "text-align: center;", required = "required", validationmessage = "Select Industry" }) .BindTo((System.Collections.IEnumerable)ViewData["Industry"]) .Events(x => x.Change("onChangeDropDown").Change("IndustryChange")) .OptionLabel("--- Select ---") .NoDataTemplateId("noDataTemplate") )验证数据源完整性
确保ViewData["Industry"]中包含当前行IndustryId对应的选项,这样下拉框才能在编辑时自动匹配并选中对应IndustryName。
内容的提问来源于stack exchange,提问作者Dimpi
相关产品推荐
相关产品推荐

