【发布时间】:2015-10-13 10:09:21
【问题描述】:
我在局部剃须刀视图上使用 $ajax jquery 函数来获取另一个局部视图以及从控制器到页面的强类型模型数据--> 显示在特定的 div 中。现在,如果数据模型数据在那里它可以工作,但如果没有模型数据,我将传递 json 响应,以便我可以检查剃刀视图以避免空异常。我的问题是 $ajax 中的 done 方法没有调用加 json 响应,我不知道我在哪里做错了
Ajax 函数
$(document).ready(function () {
/*Address*/
$.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 方法
[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
我已检查调试,在控制器中调用了 json,但它在 ajax 中警告错误
【问题讨论】:
-
$('#studentAddressDisplay').html(result);必须是$('#studentAddressDisplay').html(data);(response不存在)是错字吗? -
你的控制台有错误吗?
-
对不起我的错,但还是一样
-
不工作....错误函数被调用
-
我需要告诉ajax期待json吗???
标签: c# ajax asp.net-mvc jquery-ajaxq