MVC4:当控制器中发生错误时,在JQuery模式中显示错误
我想在控制器中发生错误时将partialview显示为jQuery模式。提交表单后,我需要检查验证,如果验证失败,则在jQuery模型弹出窗口中显示部分视图 Edit.cshtmlMVC4:当控制器中发生错误时,在JQuery模式中显示错误,jquery,asp.net-mvc,asp.net-mvc-3,asp.net-mvc-4,Jquery,Asp.net Mvc,Asp.net Mvc 3,Asp.net Mvc 4,我想在控制器中发生错误时将partialview显示为jQuery模式。提交表单后,我需要检查验证,如果验证失败,则在jQuery模型弹出窗口中显示部分视图 Edit.cshtml 我们如何做到这一点?您可以使用Ajax请求发表文章,并将Json从操作返回到视图,然后在jquery Ajax调用的回调中在对话框中显示错误: [HttpPost] [ValidateAntiForgeryToken] public ActionResult Edit(Activity activity) {
我们如何做到这一点?您可以使用Ajax请求发表文章,并将Json从操作返回到视图,然后在jquery Ajax调用的回调中在对话框中显示错误:
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Edit(Activity activity)
{
//Do stuff
return Json(flag, JsonRequestBehavior.AllowGet);
}
function showDialog(message){
var $dialog = $('<div></div>')
.html(message)
.dialog({
autoOpen: false,
modal: true,
height: 625,
width: 500,
title: "Login Result"
});
$dialog.dialog('open');
}
flag
只是一个简单的类,它包含错误和成功字符串/布尔值,用于向用户显示结果
public class Flag
{
public bool Success { get; set; }//determine whether the call succeeded or not
public string Error { get; set; }//show some detailed error message
}
然后在视图中显示以下ajax请求:
function serializeAndSendLoginForm() {
var form = $("#logOnForm").serialize();
$.ajax({
url: '<%: Url.Action("LogOn","Account",new{area="Security"}) %>',
type: 'POST',
data: form,
success: function (data) {
if (data.Success) {
//do more stuff
} else {
showDialog(data.Error);
}
},
error: function () {
alert("error");
}
});
}
函数serializeAndSendLoginForm(){
var form=$(“#logOnForm”).serialize();
$.ajax({
url:“”,
键入:“POST”,
数据:表格,
成功:功能(数据){
if(data.Success){
//多做事
}否则{
showDialog(data.Error);
}
},
错误:函数(){
警报(“错误”);
}
});
}
用于加载对话框的函数:
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Edit(Activity activity)
{
//Do stuff
return Json(flag, JsonRequestBehavior.AllowGet);
}
function showDialog(message){
var $dialog = $('<div></div>')
.html(message)
.dialog({
autoOpen: false,
modal: true,
height: 625,
width: 500,
title: "Login Result"
});
$dialog.dialog('open');
}
函数显示对话框(消息){
变量$dialog=$('')
.html(信息)
.对话({
自动打开:错误,
莫代尔:是的,
身高:625,
宽度:500,
标题:“登录结果”
});
$dialog.dialog('open');
}
您可以使用某个容器类(而不是我的答案中的标志变量)从控制器向视图返回其他信息,并向用户显示其他错误详细信息