Javascript 如果出现错误,请返回模式
我在索引页面前面有一个模态窗口,当模型有效时,我重定向到索引视图,但当模型无效时,我希望它保持在模态上,我该怎么做Javascript 如果出现错误,请返回模式,javascript,jquery,asp.net,asp.net-mvc,asp.net-mvc-4,Javascript,Jquery,Asp.net,Asp.net Mvc,Asp.net Mvc 4,我在索引页面前面有一个模态窗口,当模型有效时,我重定向到索引视图,但当模型无效时,我希望它保持在模态上,我该怎么做 [HttpPost] [ValidateAntiForgeryToken] public ActionResult Create( [Bind(Include = "Id,name,user,test")] Roles goles) { if (ModelState.IsValid) { db.GCollection.Add(groupRol
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create(
[Bind(Include = "Id,name,user,test")] Roles goles)
{
if (ModelState.IsValid)
{
db.GCollection.Add(groupRoles);
db.SaveChanges();
return RedirectToAction("Index");
}
//return View(groupRoles);
return null;
}
你必须做一些javascript 1/从控制器返回json:
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create(
[Bind(Include = "Id,name,user,test")] Roles goles)
{
if (ModelState.IsValid)
{
db.GCollection.Add(groupRoles);
db.SaveChanges();
return Json(new {Success=true, Redirect=@Url.Action("Index")});
}
var errors = ModelState.Values.SelectMany(v => v.Errors.Select(e => string.IsNullOrEmpty(e.ErrorMessage) ? e.Exception.Message : e.ErrorMessage)).ToArray();
return Json(new {Success=false, Errors = errors});
}
2/使用ajax提交表单。你应该使用
谢谢,但是调用我的模态的地方在哪里,在模态中我有错误,因为我添加了错误validations@HolgerTans很抱歉,我不理解你的评论。目前我有一个模式弹出窗口,其中包含一些验证,我在控制器中进行验证,如果模态正常,我将数据返回到索引页如果不希望模态不会关闭并显示错误,我该如何做?@HolgerTans我在操作中添加了一行,以自动从ModelState检索错误,我该如何调用模态?
$("#yourForm").ajaxForm({
success:function(data){
if(data.Success===true){
// in case of success we redirect
document.location.href= data.Redirect;
}
else{
// Action in case of errors
alert(data.Errors.join(", "));
}
}
});