0

我有以下具有两个下拉菜单和一个按钮的视图:

@model RoomReservation.Webb.Models.HomeViewModel

@{
    ViewBag.Title = "";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

<h2>Index</h2>

<fieldset>
    <legend>Select a city and a hotel</legend>
    <br />
    <br />
    <br />
    @Html.DropDownListFor(x=>x.City_DDL_ID, new SelectList(Model.AllCities, "Value", "Text"),"...pick a city..." )
    <br />
    <br />
    @Html.DropDownListFor(x => x.Hotel_DDL_ID, Enumerable.Empty<SelectListItem>(), "...pick a hotel...", new { disabled = "disabled"})
    <br />
    <br />
    <input id="SearchButton" type="submit"  onclick="window.location.href='/Home/SearchForRooms'" disabled="disabled"/> 
</fieldset>

<script type="text/javascript" language="javascript">
    $('#City_DDL_ID').change(function () {
        var selectedCity = $(this).val();
        if (selectedCity != null && selectedCity != '-1') {
            $.getJSON('@Url.Action("GetHotels")', { id: selectedCity }, function (hotels) {
                var hotelsSelect = $('#Hotel_DDL_ID');
                hotelsSelect.empty();
                $.each(hotels, function (index, hotel) {
                    hotelsSelect.append($('<option/>',
                    {
                        value: hotel.Value,
                        text: hotel.Text
                    }));
                });
                $('#Hotel_DDL_ID').attr('disabled', false);
                $('#SearchButton').attr('disabled', false);

            });
        }
    });
</script>

<script type="text/javascript" language="javascript">
 function onsubmitclcik() {
    var SecondDrpId = $('#Hotel_DDL_ID').val();
    window.location.href = '/Home/SearchForRooms?parameter=' + SecondDrpId;

}

我想从第二个下拉列表中获取一个值,以便我可以将它作为参数提供给从按钮属性“onclick”中获得的方法。它现在正在工作(在脚本上方)。但我的动作仍然得到空参数。这是代码:

public ActionResult SearchForRooms(int SecondDrpId) 
    {
        if (SecondDrpId > -1)
            return View(); //Here goes some model, not important
        else
            return RedirectToRoute("Home/Index");
    }

这是 Chrome 参数:请求 URL:http://localhost:22191/Home/SearchForRooms?parameter=3 请求方法:GET 谢谢**

4

2 回答 2

0

您可以使用以下代码来解决您的问题。

<input id="SearchButton" type="submit" onclick="onsubmitclcik();"  disabled="disabled"/>

编写一个javascript函数来调用你的下一个网址,

function onsubmitclcik() {
        var SecondDrpId = $('#Hotel_DDL_ID').val();
        window.location.href = '/Home/SearchForRooms?parameter=' + SecondDrpId;
}
于 2012-11-01T11:18:39.563 回答
0

为什么通过设置window.location.href您的操作网址来调用您的操作?这样,不会向您的操作发送单个参数。将参数添加到 url 并将其命名为您的操作参数的命名。在您的示例中,必须调用参数SecondDrpId

onclick="window.location.href='/Home/SearchForRooms?SecondDrpId=' + $('#Hotel_DDL_ID').val()"

或者将您的下拉菜单嵌入到表单中并将其发布到您的操作方法中。

于 2012-11-01T12:11:58.917 回答