简体   繁体   English

DropDownListFor带有值的默认项

[英]DropDownListFor Default Item with value

i have a @Html.DropdownlistFor dropdownlist. 我有一个@Html.DropdownlistFor dropdownlist。 I need to add default value to it. 我需要为其添加默认值。 Here I give that dropdown default name as a cash I need to give it an option value here. 在这里,我将该下拉默认名称用作现金,我需要在此处为​​其提供一个选项值。

Name : CASH Value : -1 名称:现金价值:-1

Here is my Code 这是我的代码

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

As answered here :- How to set default value for ASP.NET MVC DropDownList from model 如这里回答:- 如何从模型设置ASP.NET MVC DropDownList的默认值

You can't set the default value using Html.DropDownList, if you want to have a default value, the property itself should have a default value. 您不能使用Html.DropDownList设置默认值,如果要使用默认值,则属性本身应具有默认值。

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

Then, when the drop down list renders, as long as "UK" is actually a value for one of the options, it will be automatically set to that. 然后,当呈现下拉列表时,只要“ UK”实际上是其中一个选项的值,它将自动设置为该值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM