Reputation: 5109
I have a really strange issue on one of my edit pages. The dropdown below never get's the selected value.
@Html.DropDownListFor(model => model.Bodymaterial, (IEnumerable<SelectListItem>)ViewBag.Bodymaterial)
However the following dropdown on the same page does get the selectedvalue.
@Html.DropDownListFor(model => model.Unit, (IEnumerable<SelectListItem>)ViewBag.Units)
The first one is rendered like this.
<select id="Bodymaterial" name="Bodymaterial">
<option value="EDTA">EDTA</option>
<option value="SWAB">SWAB</option>
<option value="UR">UR</option>
<option value="FAE">FAE</option>
<option value="IOF">IOF</option>
<option value="SKIN">SKIN</option>
<option value="HEP">HEP</option>
<option value="CLOT">CLOT</option>
<option value="CIT">CIT</option>
<option value="CRM">CRM</option>
<option value="CSF">CSF</option>
<option value="RNA">RNA</option>
<option value="MILK">MILK</option>
<option value="BUC">BUC</option>
</select>
The second on is rendered as it should be.
<select data-val="true" data-val-required="The Unit field is required." id="Unit" name="Unit"><option selected="selected" value="µl">µl</option>
<option value="ml">ml</option>
<option value="l">l</option>
<option value="µg/ml">µg/ml</option>
</select>
Why the second one is rendered correct and the first one not? Please help....
Upvotes: 1
Views: 1499
Reputation: 5109
The solution is to change the ViewBag.Bodymaterial
property to ViewBag.Bodymaterials
. Somehow the Html.DropDownListFor doesn't understand a thing of it when your Viewbag property name matches the model property name.
So the solution is. Make sure your Viewbag property isn't the same as your modelproperty. Hope this will save you guys a lot of hours....
Upvotes: 2