如何在MVC中为@HTMLdropdownlistfor设置默认值

how to make default value for @HTMLdropdownlistfor in MVC

我正在传递一个 viewbag 以作为选择列表查看,如下所示

    ViewBag.Currency = new SelectList(db.portal_lookup.Where(c => c.lookup_type 
== "FND_CURRENCY").OrderBy(m => m.lookup_description), "lookup_id", "lookup_description");

在视图中我展示如下

     @Html.DropDownListFor(model => model.currency_code,
(SelectList)@ViewBag.Currency,"",new {  @class = "m-wrap" })

默认情况下,此下拉列表的值应为"IND"我该如何设置?

你最好的选择可能是填充你传递给视图的模型,假设你正在传递一个模型,从而设置

model.currency_code = "IND".

或者,您可以使用新 SelectList 的重载,它允许您指定选定的值:

SelectList Constructor (IEnumerable, String, String, Object) Initializes a new instance of the SelectList class by using the specified items for the list, the data value field, the data text field, and a selected value.

ViewBag.Currency = new SelectList(db.portal_lookup.Where(c => c.lookup_type == "FND_CURRENCY").OrderBy(m => m.lookup_description), "lookup_id", "lookup_description", "IND");

希望对您有所帮助。