0
我填充一個小套在我的模型列表,下拉列表中的項目:ASP MVC3 DropDownListFor空白默認選項
SecSellerCodes = (from s in db.DropDownValues
where s.Field.Equals("SecSellerCd") &&
s.DisplayPage.Equals("Agent Create")
select s).Select(x => new SelectListItem
{
Text = x.DisplayValue,
Value = x.AllowedValue
}).Distinct().ToList();
我希望用戶能夠從項目的列表中選擇從上面的查詢中拉出來,再加上列表頂部的空白選項,以防他們不希望填充這個特定字段。
使用以下幫助程序,一旦頁面加載/用戶保存到數據庫,此工作正常。但是,如果他們需要重新加載頁面並進行調整,則始終選擇空白string.Empty
項目(不管表格中的值)。
@Html.DropDownListFor(model => model.SecSellerCd, Model.SecSellerCodes, string.Empty)
任何想法,用什麼樣的輔助工具/技術來完成這個?
如果屬性'SecSellerCd'的值與'SelectList'中的一個值(即'x.AllowedValue')匹配,那麼它將被選中。你確定有匹配的值嗎? – 2014-09-30 21:51:29
是的。在這種情況下,顯示和允許值是相同的。我已驗證該字段中存在有效值。 – NealR 2014-09-30 21:57:11
奇怪。嘗試創建'SecSellerCodes'作爲'SelectList' - 'var values =(從db.dropDownValues中的s where ... select s); SecSellerCodes = new SelectList(values,「AllowedValue」,「DisplayValue」);' – 2014-09-30 22:05:33