How to provide default values to an ASP.Net Core DropDownList MVC

Question: How do you provide a default  "Select One" Value in a DropDownList Razor C# MVC using Asp.Net Core 3.1?

Login to See the Rest of the Answer

Just add a third parameter in the DropDownList to indicate default values. You don't need to go through a hussle of adding a default item to the List from the Server Side. If you want to go that route then that is fine too. However the easiest way is shown below:

@Html.DropDownListFor(model => model.SelectedValue, Model.YourListOfItems, "Select One Below")

- If want to implement dropdown list right in a Razor Page see the code below:

  @Html.DropDownList("USOrInternational", new List<SelectListItem>
   new SelectListItem{ Text="US", Value = "US" },
   new SelectListItem{ Text="International", Value = "International" },
},"Us or International",new {@class="form-control"})

© 2024 - ErnesTech - Privacy
E-Commerce Return Policy