如果所选值不在源中,则 DropDownList 显示空白值

DropDownList Displays Blank Value if Selected Value Not in Source

我想让下拉列表显示以前 selected 但现在从下拉列表源中删除的值。而不是显示空白。下拉列表位于网格列中。

网格:

...
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 列),用户可以 select 从 LookupCategory 中找到的项目列表。但是,如果从 LookupCategory 中删除了某个项目,则该值仍应显示在用户已经为 CurrentCategory select 编辑该值的实例中。

目前,如果某行包含不在 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)
)