简体   繁体   English

更改HTML.Dropdownlist的选项值

[英]Alter option value of HTML.Dropdownlist

I have a HTML helper for a dropdown list (shown below) 我有一个用于下拉列表的HTML帮助器(如下所示)

View: 视图:

 @Html.DropDownList("LocationID", null, new {id ="Location" })

Controller: 控制器:

  ViewBag.LocationID = new SelectList(db.Locations, "LocationID", "LocationName", job.LocationID);

Now when this renders in google chrome, it presents it like the following 现在,当它以Google chrome呈现时,它如下所示

    <select id="Location" name="LocationID">
<option selected="selected" value="2">Cleanroom</option>
<option value="1">Head Board Assembly</option>
<option value="5">Light Testing</option>
<option value="3">Mechanical Assembly</option>
<option value="6">Picture Testing</option>
<option value="4">Quality Control</option>
<option value="7">Shipping</option>
</select>

How do i alter the option value from numeric to the actual values, so for example: 我如何将选项值从数字更改为实际值,例如:

option value =1 should actually equal- option value:Head Board Assembly 选项值= 1应该实际上等于-选项值:主板组装

The reason for this is for a jquery and to save further confusion down the line id like the value to be the actual value, saving anyone working on this code in future the frustration of going back and forth between linking numeric values to the actual value 这样做的原因是为了进行jquery,并进一步避免将行ID混淆,例如将值作为实际值,从而避免了将来使用此代码的任何人将来将数字值链接到实际值之间来回的麻烦

Thanks in advance :) 提前致谢 :)

Then you have to pass LocationName property in both ValueField and TextField in the SelectList Constructor: 然后,您必须在SelectList构造函数的ValueField和TextField中传递LocationName属性:

new SelectList(db.Locations,"LocationName", "LocationName", job.LocationName);

but you should be using LocationID as value of Dropdownlist. 但您应该使用LocationID作为Dropdownlist的值。

您必须两次传递位置名称,而不是LocationID和locationName

ViewBag.LocationID = new SelectList(db.Locations, "LocationName", "LocationName", job.LocationID);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM