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?

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"})

Edit this Article
DropDownList Razor C MVC using Asp.Net Core 3.1 Technology published
Was this page helpful?
Yes Yes! this solution to the problem was helpful. No No! this solution to the problem was not helpful.. Leave ErnesTech Feedback.Feedback
If you log in, you will be notified when someone leaves a comment.

Other users would like to know if this solution helped you.

Your Session is Ending
Login to Continue

© 2021 - ErnesTech - Privacy