MVC4下拉列表

时间:2013-04-21 02:06:51

标签: asp.net-mvc-4 drop-down-menu

我需要将三个下拉列表中的值传递给控制器​​,当用户从dropdownlist.i中的任何一个中选择某个值时,尝试这样但仅从选定的下拉列表中传递的值其他值为空值,任何帮助将不胜感激

查看

      @@using (Html.BeginForm("GetFilterValues","Home",FormMethod.gET))
          {   
            @Html.DropDownList("FClass", ViewBag.Market as SelectList,  new { id = "Market" , onchange = "$(this).parents('form').submit();" })
           }

      @using (Html.BeginForm("GetFilterValues","Home",FormMethod.Get))
                    {
                          @Html.DropDownList("FClass", ViewBag.Class as SelectList,  new { id = "FClass" , onchange = "$(this).parents('form').submit();" })
                      }

              </td>
                        <td>
                 @Html.Label("Status")
                            </td>
               <td>
                    @using (Html.BeginForm("GetFilterValues","Home",FormMethod.Get))
                    {
                                                 @Html.DropDownList("Status", ViewBag.Status as SelectList,  new { id = "Status" , onchange = "$(this).parents('form').submit();" })

                    }

CONTROLLER             [HTTPGET]

      public void GetFilterValues()
       {
        string  market = this.Request.Form.Get("Market");
        string fclass = this.Request.Form.Get("FClass");
        string status = this.Request.Form.Get("Status");
        }

1 个答案:

答案 0 :(得分:0)

通过POST尝试单个表单,并使用像这样的FormCollection按名称提取值...

查看:

@using (Html.BeginForm("GetFilterValues","Home", FormMethod.Post))
    {     
          @Html.DropDownList("nameMarket", ViewBag.Market as SelectList,  new { id = "Market" , onchange = "$(this).parents('form').submit();" })
          @Html.DropDownList("nameClass", ViewBag.Class as SelectList,  new { id = "FClass" , onchange = "$(this).parents('form').submit();" })
          @Html.DropDownList("nameStatus", ViewBag.Status as SelectList,  new { id = "Status" , onchange = "$(this).parents('form').submit();" })
    }

<强>控制器:

[HttpPost]
public void GetFilterValues(FormCollection collection) {
    string  market = Convert.ToString(collection["nameMarket"]);
    string fclass = Convert.ToString(collection["nameClass"]);
    string status = Convert.ToString(collection["nameStatus"]);
}