I have a select list and I want to take the value selected and pass it to de controller, How can I do that?
this is my code
<select id="SelectImageType" onchange='submit();'>
<optio开发者_运维问答n value="0">Select Image Type</option>
<%foreach (var type in Model.ImageTypes)
{ %>
<option value="<%=type.Key%>"><%=type.Name%></option>
<%} %>
</select>
Thanks
The best why to do it is to use a DropDownListFor and then add the result into the model.
View:
<%: Html.DropDownListFor(m => m.SelectedImageType, new SelectList((IEnumerable)Model.ImageTypes, "Key", "Name"))%>
Controller:
ActionResult YourActionName(Model model){
var selectedImages = m.SelectedImageType;
}
Use name and in your controller use form collection to collect selected values
<select id="SelectImageType" onchange='submit();' name ="image">
<option value="0">Select Image Type</option>
<%foreach (var type in Model.ImageTypes)
{ %>
<option value="<%=type.Key%>"><%=type.Name%></option>
<%} %>
</select>
In your controller action
ActionResult YourActionName(FormCollection collection){
var selectedImages = collection["image"];
}
精彩评论