1

i have a @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

Here is my Code

 @Html.DropDownListFor(m => m.Customer_Supplier_CustomerSupplierId, (SelectList)ViewBag.ListOfCustomers, "CASH", new { placeholder = "CustomerName", id = "CustomerName", @class = "form-control" })
Lamloumi Afif
  • 8,941
  • 26
  • 98
  • 191
TechGuy
  • 4,298
  • 15
  • 56
  • 87

1 Answers1

1

As answered here :- How to set default value for ASP.NET MVC DropDownList from model

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.

 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.

Community
  • 1
  • 1
Neel
  • 11,625
  • 3
  • 43
  • 61