简体   繁体   中英

Alter option value of HTML.Dropdownlist

I have a HTML helper for a dropdown list (shown below)

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

    <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

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

Thanks in advance :)

Then you have to pass LocationName property in both ValueField and TextField in the SelectList Constructor:

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

but you should be using LocationID as value of Dropdownlist.

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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