Alvykun
Alvykun

Reputation: 173

@Html.DropDownListFor using ViewBag SelectedListItem the selected item is not shown

My code is the following:

Controller:

foreach (Employee engineer in engineers)
{                       
       if (!model.Customer.PrimaryEngineerId.Equals(engineer.EmployeeId)) 
       {
            engineersListPrimary.Add(new SelectListItem { Text = engineer.FirstName + ' ' + engineer.LastName, Value = engineer.EmployeeId, Selected = false});
       }
       else
       {
            engineersListPrimary.Add(new SelectListItem { Text = engineer.FirstName + ' ' + engineer.LastName, Value = engineer.EmployeeId, Selected = true });
       }                 
}
ViewBag.engineersListPrimary = engineersListPrimary;
return View(model);

View:

@Html.DropDownListFor(m => m.Customer.PrimaryEngineer, (IEnumerable<SelectListItem>)ViewBag.engineersListPrimary, new { style = "width:100%;"})

After searching a lot and trying a bunch of different ways I couldn't manage to print the selected value wished.

Problem:

The selected value is not shown in the DropDownListFor used in the View.

After debugging I have checked that the engineersListPrimary is correctly filled (the Selected = true row is inserted correctly) so the problem must remain in the View.

Any suggestions?? Thanks for your answers!

Upvotes: 3

Views: 4539

Answers (3)

Ehsan Sajjad
Ehsan Sajjad

Reputation: 62488

The best way what i see is to just do it using this overload of SelectList().

Replace your foreach loop code with this and you are good to go:

ViewBag.engineersListPrimary = new SelectList(engineers,
                                             "EmployeeId",
                                             "FirstName",
                                             model.Customer.PrimaryEngineerId);
return View(model);

and in View:

@Html.DropDownListFor(m => m.Customer.PrimaryEngineer, 
                           ViewBag.engineersListPrimary as SelectList, 
                           new { style = "width:100%;"})

MSDN Docs:

public SelectList(
    IEnumerable items,
    string dataValueField,
    string dataTextField,
    Object selectedValue
)

See SelectList class on MSDN

Upvotes: 1

Andrei
Andrei

Reputation: 56688

The problem is in this expression:

m => m.Customer.PrimaryEngineer

DropDownListFor expects this lambda to give the property that you are trying to set by this input control. In your case this is not the m.Customer.PrimaryEngineer but m.Customer.PrimaryEngineerId, so this should fix it:

m => m.Customer.PrimaryEngineerId

Also note that there is no need to set Selected property of your list items while using DropDownListFor, as this helper can derive selected item based on the provided model value.

Upvotes: 2

husnain_sys
husnain_sys

Reputation: 571

Try something like this;

@Html.DropDownListFor(model => model.SelectedID, new SelectList(model.PrimaryEngineer, "Value", "Text"), "optionalLabel",  new { style = "width:100%;"})

Upvotes: 0

Related Questions