2012-04-19 11 views
2

在編輯觀點: 我傳遞ViewBag列表中DDLMVC3下拉列表重載和設定值

控制器:

ViewBag.CountryList = new SelectList(db.Country, "CountryCode", "Desc"); 
在編輯視圖

: 如何將viewbag分配給DDL和設置來自模型的值

@Html.EditorFor(model => model.CountryCode) <- contains the Desc Value, currently just shows in a textbox! 

thx!

+1

使用'@ Html.DropDownListFor'。如果你閱讀了C#指定的你需要的參數,它的自我解釋。事實上,你已經在viewbag中擁有了selectList,這意味着你幾乎已經完成了所有工作。 – Doomsknight 2012-04-19 16:45:39

回答

3

use @ Html.DropDownListFor。如果你閱讀了C#指定的你需要的參數,它的自我解釋。事實上,你已經在viewbag中擁有了selectList,這意味着你幾乎已經完成了所有工作。

@Html.DropDownListFor(model => model.CountryCode, ViewBag.CountryList); 

如果那不設置任何理由正確的值,你也可以使用

@Html.DropDownList("CountryCode", ViewBag.CountryList, new { @value = @Model.CountryCode });