MVC3 DropDownlistFor SelectedValue 的
我的编辑页面之一上有一个非常奇怪的问题。下面的下拉菜单永远不会获得所选值。
@Html.DropDownListFor(model => model.Bodymaterial, (IEnumerable<SelectListItem>)ViewBag.Bodymaterial)
但是,同一页面上的以下下拉列表确实获取了所选值。
@Html.DropDownListFor(model => model.Unit, (IEnumerable<SelectListItem>)ViewBag.Units)
第一个是这样渲染的。
<选择 id="Bodymaterial" name="Bodymaterial"> <选项值=“EDTA”>EDTA <选项值=“SWAB”>SWAB <选项值=“UR”>UR <选项值=“FAE”>FAE <选项值=“IOF”>IOF <选项值=“皮肤”>皮肤 <选项值=“HEP”>HEP <选项值=“CLOT”>CLOT <选项值=“CIT”>CIT <选项值=“CRM”>CRM <选项值=“CSF”>CSF <选项值=“RNA”>RNA <选项值=“牛奶”>牛奶 <选项值=“BUC”>BUC
第二个 on 按其应有的方式呈现。
为什么第二个渲染正确而第一个渲染不正确?请帮忙....
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....
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
解决方案是将
ViewBag.Bodymaterial
属性更改为ViewBag.Bodymaterials
。不知何故,当您的 Viewbag 属性名称与模型属性名称匹配时, Html.DropDownListFor 无法理解它。所以解决办法是。确保您的 Viewbag 属性与您的 model 属性不同。希望这能为你们节省很多时间......
The solution is to change the
ViewBag.Bodymaterial
property toViewBag.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....