How to make DropDownListFor display the value that is in SelectedStatusId ? Now displays the first in a row regardless of the value of SelectedStatusId

In the controller

 List<SelectListItem> Statuses = new List<SelectListItem>(); Statuses.Add(new SelectListItem() { Text = "Passed", Value = "PSS" }); Statuses.Add(new SelectListItem() { Text = "Failed", Value = "FL" }); Statuses.Add(new SelectListItem() { Text = "Blocked", Value = "BLCK" }); Statuses.Add(new SelectListItem() { Text = "Not run", Value = "NR" }); ViewBag.Statuses = new SelectList(Statuses, "Value", "Text"); 

In view

 @Html.DropDownListFor( x => x.SelectedStatusId, (SelectList)ViewBag.Statuses, new { @class = "form-control" } ) 

    1 answer 1

    You have an explosive mixture of several errors that are superimposed on each other - and therefore you can not figure it out.

    Suppose we have our own custom status:

     public class MyStatus { public int Id { get; set; } public string Title { get; set; } } 

    And it can usually be stored in the database, but we will create it manually and pass it into the view:

     public ActionResult Index() { var statuses = new[] { new MyStatus {Id = 1, Title = "New"}, new MyStatus {Id = 2, Title = "Open"}, new MyStatus {Id = 3, Title = "Stopped"}, }; var model = new SomeModel {SelectedStatusId = 2, Statuses = statuses}; return this.View(model); } 

    (I really do not like the transfer via ViewBag and I recommend everyone to use strongly typed models)

    Your web form will look something like this:

     @model MyApp.WebUI.Models.SomeModel @using (Html.BeginForm("", "Home")) { <div> @Html.DropDownListFor( x => x.SelectedStatusId, Model.Statuses.Select(x => new SelectListItem { Text = x.Title, Value = x.Id.ToString(), Selected = x.Id == Model.SelectedStatusId }), new { @class = "form-control" } ) </div> } 

    An example is chosen specifically for you.

    First, I have a SelectedStatusId - this is an honest int and in the model is also an int. You SelectedStatusId it is not clear that, and in SelectListItem - the line.

    Secondly, look at the definition of the class SelectListItem - I filled in manually Selected comparing the value of Model.SelectedStatusId and Id of the current item.

    It works. This will be the first example I want to show you.

    Further. Instead of manually working with SelectListItem , creating it through lambda - you can use the ready-made SelectList helper. I do not change anything in the controller, and the presentation will be like this:

     @using (Html.BeginForm("", "Home")) { <div> @Html.DropDownListFor( x => x.SelectedStatusId, new SelectList(Model.Statuses, nameof(MyStatus.Id), nameof(MyStatus.Title), Model.SelectedStatusId), new { @class = "form-control" } ) </div> } 

    It works too. We in SelectList transfer the data, say which fields to read and say which element is selected.

    Note. Your statuses already have Text and Value fields that match what SelectListItem expects, I specifically selected an example to get away from it (it only confuses).

    PS You used the framework service class instead of the domain class. SelectListItem is a class from the presentation layer, let the view itself decide how to display.