我有下一个代码: 在我的CSHTML文件中
@Ajax.ActionLink("GenerateExcel", "DownloadAsync", "SomeController", null, new AjaxOptions() { HttpMethod = "GET", OnSuccess = "SuccessLink()", OnFailure = "onError(xhr, status, error)" }, new { id = "ExcelLink" })
JS:
function onError(xhr, status, error) {
showMessage(2, xhr.responseJSON.Message);
}
CS:
if (ModelState.IsValid)
{
//some code here
}
else
{
var error = string.Join("\n", ModelState.Values.SelectMany(x => x.Errors.Select(y => y.ErrorMessage)));
Response.StatusCode = (int)HttpStatusCode.NotAcceptable;
return Json(new {Success = false, Message = error});
}
还有一个错误,提示“无法读取未定义的属性'消息'”
responseJSON 也未定义
和 error 参数等于“内部服务器错误”,而不是“不可接受”
在@ Ajax.BeginForm中,此代码可以正常工作。 例如:
@using (Ajax.BeginForm("SomeMethod", "SomeController", new AjaxOptions {UpdateTargetId = "reportWrapper", HttpMethod = "Post", LoadingElementId = "loading", OnSuccess = "fillGeneratedTime()", OnFailure = "onError(xhr, status, error)"}))