开发者

get value from select list to controller

开发者 https://www.devze.com 2023-04-10 16:36 出处:网络
I have a select list and I want to take the value selected and pass it to de controller, How can I do that?

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"];
     }
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号