Home > Articles > Details

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?


Please Login to see the rest of the answer
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"})

If you log in, you will be notified when someone leaves a comment.

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


© 2022 - ErnesTech - Privacy