[英]How to load model errors into modal dialog
使用 javascript 提交表单后,是否可以将模型状态错误加载到同一个模式对话框?
我的代码是这样的:
控制器:
public ActionResult Create(MyModel model){
if(ModelState.isValid){
// DB Save
return RedirectToAction("Index");
}
else{
return View(model);
}
}
阿贾克斯方法
$.ajax({
type: 'POST',
url: '/Receipt/Create',
cache: false,
data: $("#CreateForm").serialize(),
success: function (e) { window.location="/Controller/Action"; },
error: function (e) { e.preventDefault(); /*Code here to load model error into page*/ }
});
我今天已经解决了这个问题,就像这样
public ActionResult Create(MyModel model){
if(ModelState.isValid){
// DB Save
return RedirectToAction("Index");
}
else{
return PartialView("_myPartialForm",model);
}
}
和
$.ajax({
type: 'POST',
url: '/Receipt/Create',
cache: false,
data: $("#CreateForm").serialize(),
success: function (e) {
if(e.Valid){
window.location="/Controller/Action";}
else{
return false;
} },
error: function (e) { e.preventDefault();$("#mymodal").load(e) }
});
就像 jmrnet 所说的那样。 谢谢
我能够通过使用带有UpdateTargetId
AjaxOption 的Ajax.BeginForm
方法来完成此操作。 这是我使用的代码。 它并不完全适合您正在做的事情,但它应该为您指明正确的方向。
在视图中:
@using (Ajax.BeginForm(new AjaxOptions(){ UpdateTargetId="loginresult" }))
{
<b>User:</b><br />
@Html.TextBoxFor(m => m.UserName)<br />
<br />
<b>Password:</b><br />
@Html.PasswordFor(m => m.Password)<br />
<div id="loginresult"><br /></div>
<input id="Button1" type="submit" value="Login" class="touch-button" />
}
在控制器中:
[HttpPost]
public ActionResult Index(LoginModel model)
{
//Execute Log-in code.
//Capture any errors and put them in the model.LoginResponse property.
return PartialView("LoginResult", model);
}
在LoginResult
部分视图中:
@model MerchantMobile.Models.LoginModel
@if (String.IsNullOrEmpty(Model.LoginResponse))
{
Html.RenderPartial("_AjaxRedirect", Url.Content("~/Home/Activity"));
}
else
{
<div id="loginresult">
<div style="color: Red; font-weight: bold;">
@Model.LoginResponse
</div>
</div>
}
您可以轻松地将loginresult
<div>
替换为 jquery ui 用来弹出模式对话框的结果,而不仅仅是在 div 中显示一些文本。
希望这可以帮助!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.