0

2 つのドロップダウンと 1 つのボタンがある次のビューがあります。

@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」から取得したメソッドにパラメーターとして渡すことができるように、2 番目のドロップダウンから値を取得したいと考えています。現在は機能しています (上記のスクリプト)。しかし、私のアクションはまだ null パラメータを取得します。コードは次のとおりです。

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"/>

次の URL を呼び出す 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に設定してアクションを呼び出すのはなぜですか? そうすれば、単一のパラメーターがアクションに送信されることはありません。パラメーターを URL に追加し、アクション パラメーターの名前と同じように名前を付けます。あなたの例では、パラメータを呼び出す必要がありますSecondDrpId

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

または、ドロップダウンをフォーム内に埋め込み、アクション メソッドにポストします。

于 2012-11-01T12:11:58.917 に答える