I am trying to fill the state dropdown when user selects a country in country dropdown. I am using json. Now, i am getting the error respose - [object XMLHttpRequest].
I struck with this for a lot of time. Please don't post or provide anymore new methods, as I have followed a lot of them. So, please tell me if there is anything wrong with the following code.
Thanks in advance
My view code is
<% using (Html.BeginForm()) {%>
<%: Html.ValidationSummary(true) %>
<fieldset>
<legend>Fields</legend>
<div class="editor-label">
<%: Html.LabelFor(model => model.Name) %>
</div>
<div class="editor-field">
<%: Html.TextBoxFor(model => model.Name) %>
<%: Html.ValidationMessageFor(model => model.Name) %>
</div>
<div class="editor-label">Country</div>
<div class="editor-field">
<%: Html.DropDownList("country", ViewData["countries"] as SelectList, "Select Contry")%>
</div>
<div class="editor-label">State</div>
<div class="editor-field">
<select id="state"></select>
</div>
<p>
<input type="submit" value="Create" />
</p>
</fieldset>
<% } %>
<div>
<%: Html.ActionLink("Back to List", "Index") %>
</div>
<script type="text/javascript" src="../../Scripts/jquery-1.4.1.js"></script>
<script type="text/javascript">
$(document).ready(function () {
$("#country").change(function () {
if ($("#country").val() != "") {
$.ajax({
url: 'http://localhost:52970/State/GetState',
data: { Sel_Country: $("#country").val() },
cache: false,
type: "POST",
success: function (data) {
alert(data.length);
var markup = "<option value='0'>Select City</option>";
for (var x = 0; x < data.length; x++) {
markup += "<option value=" + data[x].Value + ">" + data[x].Text + "</option>";
}
$("#state").html(markup).show();
},
error: function (reponse) {
alert("error : " + reponse);
}
});
}
});
});
My GetState method:
public JsonResult GetState(string Sel_Country)
{
//stateRepository db = new CountryService();
JsonResult result = new JsonResult();
var vStateList=stateRepository.GetStateByCountryId(Convert.ToInt32(Sel_Country));
result.Data = vStateList;
result.JsonRequestBehavior = JsonRequestBehavior.AllowGet;
return result;
}