从下拉列表中将2个参数传递给控制器​​。

时间:2018-06-20 12:45:40

标签: jquery asp.net-mvc

我在页面上有2个下拉列表,我需要的是当两个下拉列表都选择了值时,将该值传递给控制器​​。以及如何阻止\禁用新请求,直到前一个未完成?

这里有一些代码。 查看:

    @using (Ajax.BeginForm("IndexStb", "GroupStatistics", new AjaxOptions()))
    {
        @Html.DropDownListFor(m => m.SelectedUserEmail,
            new SelectList(Model.UserEmails, "Value", "Text"), "Choose user",
            new {@class = "form-control", id = "dropDown1"})

        <p><br/></p>

        @Html.DropDownListFor(m => m.SelectedDate,
            new SelectList(Model.Dates, "Value", "Text"), "Choose date",
            new {@class = "form-control", id = "dropDown2"})
    }

    @section Scripts
    {
        <script>
            $("#dropDown2").on("change",
                function() {

                    var date = $(this).val();

                    $("#dropDown1").on("change",
                        function() {

                            var user = $("#dropDown1").val();

                            var url = '@Url.Action("GetStatistics", "GroupStatistics")' +
                                "?selectedDate=" +
                                date +
                                "&selectedUserEmail=" +
                                user;

                            if (user !== undefined && date !== undefined) {

                                $("#partialView").load(url,
                                    function() {
                                        $('#partialView').fadeIn('fast');
                                    });
                            }
                        });
                })
        </script>
    }

控制器:

    [HttpGet]
    public ActionResult GetStatistics(string selectedDate, string selectedUserEmail)
    {
        //some actions
        return PartialView("_MemberStatisticsPartial", testRunViewModels);
    }

1 个答案:

答案 0 :(得分:0)

类似这样的东西

$("#dropDown2").on("change", doSomething)

$("#dropDown1").on("change", doSomething)

function doSomething() {
    var user = $("#dropDown1").val();
    var date = $("#dropDown2").val();
    if (user !== undefined && date !== undefined) {

        var url = '@Url.Action("GetStatistics", "GroupStatistics")' +
            "?selectedDate=" +
            date +
            "&selectedUserEmail=" +
            user;

        $.ajax({
            type: 'POST',
            url: url,
            async:false, 
            success: function (data) {
                  $("#partialView").html(data)
                  $('#partialView').fadeIn('fast');
            }
        });

    }
}