I am using the $ ajax jquery function on a partial razor to get another partial view along with strongly typed model data from the controller to the page -> display in a specific div. Now, if the data model data works there, but if there is no model data, I pass a json response so that I can check the appearance of the razor to avoid a null exception. My problem in the $ ajax method does not cause a plus json response, I do not know where I am wrong
Ajax function
$(document).ready(function () {
$.ajax({
url: '@Url.Action("DisplayStudentAddress")',
type: "GET",
cache: false
}).done(function (data, textStatus, jqXHR) {
alert(data.Response);
$('#studentAddressDisplay').html(data);
}).fail(function (jqXHR, textStatus, errorThrown) {
alert(jqXHR +" "+textStatus+" "+errorThrown);
});
});
ActionResult Method
[HttpGet]
[Authorize]
public ActionResult DisplayStudentAddress()
{
int _studentEntityID = 0;
_studentEntityID = _studentProfileServices.GetStudentIDByIdentityUserID(User.Identity.GetUserId());
Address _studentAddressModel = new Address();
_studentAddressModel = _studentProfileServices.GetStudentAddressByStudentID(_studentEntityID);
if (_studentAddressModel != null)
{
return PartialView("DisplayStudentAddress_Partial", _studentAddressModel);
}
else
{
return Json(new { Response = "Provide Your Address Detail!" });
}
}
#endregion
I have a debug check, json is called in the controller, but it warns about an error in ajax
source
share