DropDownListFor具有值的默认项

时间:2014-08-26 06:24:03

标签: c# asp.net asp.net-mvc

我有一个@Html.DropdownlistFor下拉列表。我需要为它添加默认值。在这里,我将该下拉默认名称作为现金,我需要在此处给它一个选项值。

姓名:现金 值:-1

这是我的代码

 @Html.DropDownListFor(m => m.Customer_Supplier_CustomerSupplierId, (SelectList)ViewBag.ListOfCustomers, "CASH", new { placeholder = "CustomerName", id = "CustomerName", @class = "form-control" })

1 个答案:

答案 0 :(得分:1)

在这里回答: - How to set default value for ASP.NET MVC DropDownList from model

  

如果需要,您无法使用Html.DropDownList设置默认值   要具有默认值,属性本身应具有默认值   值。

 private string country; 
 public string Country
 {
  get { return country ?? "UK"; }
  set { country = value; } 
 }
  

然后,当下拉列表呈现时,只要“UK”实际上是a   其中一个选项的值,它将自动设置为该值。