Note that there are some explanatory texts on larger screens.

plurals
  1. POWhat is wrong with this JavaScript (using Ajax call in MVC 3)?
    primarykey
    data
    text
    <p><em>Here is my JavaScript:</em></p> <pre><code>&lt;script type="text/javascript"&gt; $(function () { $("select#City_DDL_ID").change(function (evt) { if ($("select#City_DDL_ID").val() != "-1") { $.ajax({ url: "/Home/GetHotels", type: 'POST', dataType: "json", data: { id: $("select#City_DDL_ID").val() }, success: function (response) { // $('#Hotel_DDL_ID').attr('disabled', false); $("select#Hotel_DDL_ID").replaceWith(response); }, error: function (xhr) { alert("Something went wrong, pleae try again"); } }); } }); }); </code></pre> <p> <em>Here is the Index.cshtml:</em></p> <pre><code>@model RoomReservation.Webb.Models.HomeViewModel @{ ViewBag.Title = "Index"; 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;/ fieldset&gt; </code></pre> <p><em>And here is the method that ajax calls:</em></p> <pre><code>public JsonResult GetHotels(int id) { if (id &gt; 0) { var hotels = bl.ReturnAllHotelsInCity(id); return Json(hotels); } else { throw new Exception("Hotel not reachable, please try again"); } } </code></pre> <p>After selecting City in City_DDL_ID, my Hotel_DDL_ID disapperas. Can you help me, I am new in javasrcipt and ajax?</p> <p>Here is the newset script, and still not working:</p> <pre><code>&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); }); } }); &lt;/script&gt; </code></pre> <p>So, what to do?</p> <p>Thank you all for your help, this latest script of mine is working, I change the default browser to Chrome. Thank you all once again Cheers</p> <p>P.S. Admin:How to mark question as answered?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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