Need help with refreshing partial views in MVC from ajax call. In my View page.cshtml
<div id="tblOptions">
@Html.DropDownListFor(model => model.State , new SelectList(), new {id="ddlstate"})
@html.HiddenFor(model => model.optionsId)
@Html.CheckBoxFor(model => model.PrintAddress)
........
</div>
On Page load the model is filled properly and all checkboxes are correctly filled. Now onChange event of dropdownlist , I need to fill the checkboxes with new model values,
I have an ajax call, which returns jsonresult
$(document).on('change', '#ddlstate', function () {
$.ajax({
type: 'GET',
url: '/Home/CallonChange',
contentType: 'application/html; charset=utf-8',
data: { PersonCode: '@Model.PersonCode', selectedstate: $('#ddlState').val() },
dataType: 'json',
beforeSend: function (jqXHR, settings) {
$('tblOptions').html('');
}
})
.done(function (data, textStatus, jqXHR) //success callback
{
if (data != null) {
// not loading
$('tblOptions').html(data);
}
else { //display error message
}
})
.fail(function (jqXHR, textStatus, errorThrown) //error callback
{
//display error message
});
});
});
Its returning the new model in JSON result, but the partial view doesn't reload with the new values.
In Controller, I have two action controllers, one called on Load and one called on SelectionChange
[HttpGet]
public ActionResult CallOnLoad(string PersonCode, string selectedstate = "") {
ModelA a = new ModelA(PersonCode, selectedstate);
return PartialView("Home/page", options);
}
[HttpGet]
public JsonResult CallonChange(string PersonCode, string selectedstate= "")
{
ModelA a = new ModelA(PersonCode, selectedstate);
return Json(options, JsonRequestBehavior.AllowGet);
}
I am not able to reload the partial view. What am I doing wrong? I know its pretty silly somewhere but can't figure it out.
Thanks heaps.