MVC3中的多个下拉列表回发

时间:2012-05-16 19:02:08

标签: asp.net-mvc-3 razor

我在视图上有以下代码:

@using (Html.BeginForm()) {

    <div class="select-box form">
        <p>
            <strong>Select transcript name to view:</strong>
            @Html.DropDownList("license", (SelectList)ViewBag.Licenses, new { onchange = "this.form.submit();" })
        </p>
    </div>
    <div class="select-box form">
        <p>
            <strong>You may have multiple periods for each transcript name:</strong>
            @Html.DropDownList("period", (SelectList)ViewBag.Periods, new { onchange = "this.form.submit();" })
        </p>
    </div>
}

我需要实现一些逻辑,具体取决于哪个下拉列表导致回发。我想在提交表单之前添加一个隐藏的输入并通过jQuery设置控件名称的值,但我想知道是否有一种'本地'方式来执行此操作。

这是我的控制器签名:

public ActionResult Checklist(int license, int period) 

提前致谢!

2 个答案:

答案 0 :(得分:1)

我会将一个类应用于下拉列表,以便我的jQuery可以将其用作选择标准

 @Html.DropDownList("license", (SelectList)ViewBag.Licenses, new { @class="mySelect"})
 @Html.DropDownList("period", (SelectList)ViewBag.Periods, new { @class="mySelect"})
 <input type="hidden" id="source" name="source" value="" />

脚本是

$(function(){

 $(".mySelect").change(function(){
   var itemName=$(this).attr("name");
   $("#source").val(itemName);
    $("form").submit()
 });

});

答案 1 :(得分:1)

使用类似的东西。 (这里您调用的是操作方法,而不是提交表单)

哪个下拉列表导致更改将非零值传递给action方法,另一个将传递0。

 @Html.DropDownList("license", (SelectList)ViewBag.Licenses, new { onchange = "document.location.href = '/ControllerName/Checklist?license=' + this.options[this.selectedIndex].value + '&period=0'" })
 @Html.DropDownList("period", (SelectList)ViewBag.Periods, new { onchange = "document.location.href = '/ControllerName/Checklist?license=0&period=' + this.options[this.selectedIndex].value;" })

希望这有帮助!