Note that there are some explanatory texts on larger screens.

plurals
  1. POPopulating dropdown with JSON result - Cascading DropDown using MVC3, JQuery, Ajax, JSON
    primarykey
    data
    text
    <p>I've got a cascading drop-drown using mvc. Something like, if you select a country in the first-dropdown, the states of that country in the second one should be populated accordingly.</p> <p>At the moment, all seems fine and I'm getting Json response (saw it using F12 tools), and it looks something like <code>[{ "stateId":"01", "StateName": "arizona" } , { "stateId" : "02", "StateName":"California" }, etc..] ..</code></p> <p>I'd like to know how to populate my <code>second-dropdown</code> with this data. My second drop-down's id is "<code>StateID</code>". Any help would be greatly appreciated. </p> <p>Below is the code used to produce the JSON Response from the server:</p> <pre><code>[HttpPost] public JsonResult GetStates(string CountryID) { using (mdb) { var statesResults = from q in mdb.GetStates(CountryID) select new Models.StatesDTO { StateID = q.StateID, StateName = q.StateName }; locations.statesList = stateResults.ToList(); } JsonResult result = new JsonResult(); result.Data = locations.statesList; return result; } </code></pre> <p>Below is the client-side HTML, my razor-code and my script. I want to write some code inside "<code>success</code>:" so that it populates the States <code>dropdown</code> with the JSON data.</p> <pre><code>&lt;script type="text/javascript"&gt; $(function () { $("select#CountryID").change(function (evt) { if ($("select#CountryID").val() != "-1") { $.ajax({ url: "/Home/GetStates", type: 'POST', data: { CountryID: $("select#CountryID").val() }, success: function () { alert("Data retrieval successful"); }, error: function (xhr) { alert("Something seems Wrong"); } }); } }); }); &lt;/script&gt; </code></pre>
    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.
    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