C# 如果所选值不在源中,DropDownList将显示空白值

C# 如果所选值不在源中,DropDownList将显示空白值,c#,asp.net,asp.net-core,kendo-ui,telerik,C#,Asp.net,Asp.net Core,Kendo Ui,Telerik,我想允许dropdownlist显示以前选择但现在从dropdownlist源中删除的值。而不是显示空白。dropdownlist位于网格列中 网格: ... columns.ForeignKey(p => p.CurrentCategory, @Model.LookupCategory, "CategoryName", "CategoryName").Width(160); ... 模板编辑器 @using System.Collections @( Html.Kendo().Dr

我想允许dropdownlist显示以前选择但现在从dropdownlist源中删除的值。而不是显示空白。dropdownlist位于网格列中

网格:

...
columns.ForeignKey(p => p.CurrentCategory, @Model.LookupCategory, "CategoryName", "CategoryName").Width(160);
...
模板编辑器

@using System.Collections

@(
 Html.Kendo().DropDownListFor(m => m)       
        .BindTo((SelectList)ViewData[ViewData.TemplateInfo.GetFullHtmlFieldName("") + "_Data"])
        .ValuePrimitive(true)        
        .AutoWidth(true)
)
所以更详细地解释一下:CurrentCategory列是一个文本列(不是id列),用户可以从LookupCategory中找到的项目列表中进行选择。但是,如果从LookupCategory中删除了某个项目,则该值仍应显示在用户已为CurrentCategory选择该值的实例中

当前,如果某行包含不在LookupCategory列表中的CurrentCategory值,则该行显示为空


也许我必须用组合框来代替

您可以将另一个属性AllCategory添加到视图模型中,该属性包含LookupCategory和已删除项的联合。 网格将使用此属性绑定菜单选项,LookupCategory属性将用作下拉列表源

请参见下面如何在使用外键列模板时区分两者

columns.ForeignKey(p => p.CurrentCategory, Model.AllCategory, "CategoryName", "CategoryName")
       .EditorViewData(new {lookupCategory = Model.LookupCategory})
.Width(160);


@using System.Collections

@(
 Html.Kendo().DropDownListFor(m => m)       
        .BindTo((SelectList) ViewData["lookupCategory"])        
        .ValuePrimitive(true)        
        .AutoWidth(true)
)

谢谢,这个有用。没有办法避免使用ViewData并直接从模型填充吗?我不这么认为