我已經完成了這一百次,但不知道這裏發生了什麼。我有一個DropDownListFor
,我在控制器中填充像這樣爲ASP MVC指定默認值3 DropDownListFor
var mktsegments = from p in db.ChannelMarketSegment
where (p.ChannelCode != "0")
select p;
ViewBag.Pendist = new SelectList(mktsegments, "Pendist", "Pendist");
並在視圖,我試圖設置此下拉與Pendist
值列表中的默認值。
編輯: Pendist是存在於每個項目通過Linq
查詢被拉入mktsegments
的字段。
<div class="M-editor-label">
@Html.LabelFor(model => model.Pendist)<span class="req">*</span>
</div>
<div class="M-editor-field">
@Html.DropDownListFor(model => model.Pendist,
(SelectList)ViewBag.Pendist, new { onchange = "ChangeChannel()" })
</div>
但是,所有這些都將列表中的第一個值設置爲默認值。如果我嘗試添加model => model.Pendist
或Model.Pendist
作爲第三放慢參數在DropDownListFor
這樣
@Html.DropDownListFor(model => model.Pendist,
(SelectList)ViewBag.Pendist, model => model.Pendist, new { onchange = "ChangeChannel()" })
我要麼得到以下錯誤
(爲model => model.Pendist
)
Cannot convert lambda expression to type 'string' because it is not a delegate type
(爲Model.Pendist
)
'Model' confilcts with the declaration 'System.Web.Mcv.WebViewPage<TModel>.Model'
是'Pendist'對象列表還是需要選擇的值? – Jared 2013-05-01 17:46:04
'Pendist'是'mktsegments'中每個項目中包含的一個字段,我會將該信息添加到該帖子中。 Thx – NealR 2013-05-01 17:48:12