GWP
GWP

Reputation: 451

DropdownListFor doesnt show the value in the record in editor template

I have a drodownlistfor in a view that is created using a create template and it works totally fine. User can select an option and add records without any issues.

The problem is when I create a view for users to edit values of a created record, the dropdownlist doesn't show the value in the particular model that's been passed . I tried this

 @Html.DropDownListFor(m => m.GlobalMaterialId, ViewBag.MetalIds as SelectList,Model.MaterialName) 

This will show the value in the dropdownlist when the page load but its just an illusion. If user didnt touch this and did change values in another field and save, then the record will be saved with the fist value of the Dropdownlist items (not with the one in the record) . So to keep the same value, users have to manually select the item every time they load the edit view. What am I doing wrong? Please help. Thanks! Cheers!

Upvotes: 0

Views: 363

Answers (1)

Rono
Rono

Reputation: 3361

Take off "Model.MaterialName" as the last parameter so it looks like the following. You don't have to pass the selected value, it's automatically set to the value in the model when you use DropDownListFor.

@Html.DropDownListFor(m => m.GlobalMaterialId, ViewBag.MetalIds as SelectList)

Upvotes: 1

Related Questions