Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I have achieved it. Where selected value is in Hidden Filed.</p> <p>And make sure that your controller method returns json object with two fields.</p> <pre><code>&lt;script type="text/javascript"&gt; var ddlCountry; var ddlStateID; function pageLoad() { ddlStateID = $get("StateID"); ddlCountry = $get("CountryID"); $addHandler(ddlCountry, "change", bindOptions); bindOptions(); } function bindOptions() { ddlStateID.options.length = 0; var CountryID = ddlCountry.value; var stateSelected = document.getElementById('StateSelected').value; if (CountryID) { var url = "/Student/States/" + CountryID; $.getJSON(url, null, function(data) { $("#StateID").empty(); $.each(data, function(index, optionData) { if (stateSelected == optionData.ID) { $("#StateID").append("&lt;option value='" + optionData.ID + "' selected&gt;" + optionData.Name + "&lt;/option&gt;"); } else { $("#StateID").append("&lt;option value='" + optionData.ID + "'&gt;" + optionData.Name + "&lt;/option&gt;"); }; }); }); }; } </code></pre> <p></p> <p><strong>ADDED</strong> controller (My url is student/state i.e. in student controller I put following code)</p> <pre><code>public ActionResult States(int id) { var states = db.states.Select(s =&gt; new { ID = s.ID, Name = s.Name }).OrderBy(s=&gt;s.Name).ToList(); return Json(states); } </code></pre> <p>You can return any number of fields but if any of them will have a null value then it will not converted in json object. I am not sure about this but it wasn't working when I return whole state object. At that time states table was having field called deletedby and it was containing null values!. But when I return only two 'needed' fields then it is working properly!</p>
    singulars
    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.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      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