Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to capture ajax drop down list value in MVC 3 view?
    text
    copied!<p>I have the following view that has tow dropdowns and one button:</p> <pre><code>@model RoomReservation.Webb.Models.HomeViewModel @{ ViewBag.Title = ""; Layout = "~/Views/Shared/_Layout.cshtml"; } &lt;h2&gt;Index&lt;/h2&gt; &lt;fieldset&gt; &lt;legend&gt;Select a city and a hotel&lt;/legend&gt; &lt;br /&gt; &lt;br /&gt; &lt;br /&gt; @Html.DropDownListFor(x=&gt;x.City_DDL_ID, new SelectList(Model.AllCities, "Value", "Text"),"...pick a city..." ) &lt;br /&gt; &lt;br /&gt; @Html.DropDownListFor(x =&gt; x.Hotel_DDL_ID, Enumerable.Empty&lt;SelectListItem&gt;(), "...pick a hotel...", new { disabled = "disabled"}) &lt;br /&gt; &lt;br /&gt; &lt;input id="SearchButton" type="submit" onclick="window.location.href='/Home/SearchForRooms'" disabled="disabled"/&gt; &lt;/fieldset&gt; &lt;script type="text/javascript" language="javascript"&gt; $('#City_DDL_ID').change(function () { var selectedCity = $(this).val(); if (selectedCity != null &amp;&amp; selectedCity != '-1') { $.getJSON('@Url.Action("GetHotels")', { id: selectedCity }, function (hotels) { var hotelsSelect = $('#Hotel_DDL_ID'); hotelsSelect.empty(); $.each(hotels, function (index, hotel) { hotelsSelect.append($('&lt;option/&gt;', { value: hotel.Value, text: hotel.Text })); }); $('#Hotel_DDL_ID').attr('disabled', false); $('#SearchButton').attr('disabled', false); }); } }); &lt;/script&gt; &lt;script type="text/javascript" language="javascript"&gt; function onsubmitclcik() { var SecondDrpId = $('#Hotel_DDL_ID').val(); window.location.href = '/Home/SearchForRooms?parameter=' + SecondDrpId; </code></pre> <p>} </p> <p>I would like to get a value from second dropdown so that I can give it as paramater to my method f**ired from the button atribute "onclick". It is now working (above script). But my action still gets null parameter. Here is the code:</p> <pre><code>public ActionResult SearchForRooms(int SecondDrpId) { if (SecondDrpId &gt; -1) return View(); //Here goes some model, not important else return RedirectToRoute("Home/Index"); } </code></pre> <p>Here is the Chrome parameter: Request URL:http://localhost:22191/Home/SearchForRooms?parameter=3 Request Method:GET thank you**</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload