您可能必须实现自定义替换功能
1)创建一个响应模型,其中包含您的响应状态和相应的消息
public class ReposnseModel
{
public bool isSuccess {get; set;}
public string SuccessMessage {get;set;}
public string ErrorMessage {get;set;}
}
2)您的表单必须通过部分视图呈现,因此您只能返回它的内容
public ActionResult DoWork(Model model)
{
//if success:
...
return Json(new ReposnseModel{isSuccess = true, SuccessMessage = "Success"});
//if lets say model is not valid or some other error:
return PartialView("YourPartialViewForm",model)
}
使用以下内容注册 Ajax.BeginForm onSuccess 回调:
function Callback(data) {
if (data != null) {
if (data.isSuccess != undefined) { //means that the data is a serialized ReposnseModel and not a form content
if (data.isSuccess) {
alert(data.SuccessMessage);
}else
{
alert(data.ErrorMessage);
}
}
else { //otherwise data is a form content, so it needs to replace the current content
$('#formContainer').html(data);
}
}
}