MVC3 get Select Box Option Name as Well as Value
<select id="selectedSchool" size="3" multiple="multiple" name="selectedSchool">
@foreach (var item in ViewBag.pt)
{
<OPTION VALUE="@item.entityID">@item.name</OPTION>
}
</select>
on my page the user selects a school from the list of schools, each school has a unique ID which is needed and passed via the VALUE attribute, I also would like to pick up the name (item.name) that was selected how can I resolve this info from my contro开发者_如何学Pythonl Action Result?
public ActionResult ResultsSchoolsAttended(List<int> selectedSchool)
i iterate through the list of results, but I would aslo like selectedSchool.name to enter back in the db as 'display text'
how can i do this?
Well, MVC can only give you what is in the HTTP POST. And if you look at that, you will see that the <option>
inner HTML isn't there. So you have only two choices:
- Include it in the
value
in your markup, or - Look it up in your
ResultsSchoolsAttended
action based on the submitted value.
I'd pick the second, personally.
精彩评论