【发布时间】:2014-04-05 14:58:01
【问题描述】:
我们正在编写一个 ASP.NET MVC3 应用程序并希望“丰富”地完成它,例如通过使用引导程序和模式对话框。
我现在想知道如何实现模态对话框,同时又不破坏 ASP.NET 的所有优秀员工(ModelErrors,...)。
工作流程应该是这样的:
-
带有项目列表的IndexView,每个项目都有一个显示模式对话框的操作链接
@Ajax.ActionLink( "Edit", // Link Text "Edit", // ActionMethod new { id = item.Id }, // RouteValues new AjaxOptions { HttpMethod = "Get", OnBegin = "modal.showModalDiv()", InsertionMode = InsertionMode.Replace, UpdateTargetId = "modal-div", OnSuccess = "modal.ajaxSuccess()" }, new { data-toggle = "edit-modal" } // HTML-Attributes ) -
模态对话框(使用 css 样式的简单 div)呈现编辑视图(从控制器操作方法返回)
[HttpGet] public ActionResult Edit(int id) { // Load Data and create Model var model = new ... return PartialView(model); } -
编辑视图中的表单可用于编辑项目,包括客户端验证
@{ AjaxOptions ajaxOptions = new AjaxOptions() { HttpMethod = "Post", OnSuccess="modal.hideModalDiv()" }; } @using (Ajax.BeginForm("Edit"), ajaxOptions){ ... element to edit item ... <input type="submit" value="submit" /> } -
当提交编辑控制器方法识别出错误(未被客户端验证捕获)时,页面应该再次显示模型错误。否则应该显示索引页面或刷新表格并关闭模式对话框。
[HttpPost] public ActionResult Edit(Guid id, ItemModel model) { try{ ...Save Item ... return RedirectToAction("Index") } catch (Exception ex){ ModelState.AddModelError("", "An error occured") return PartialView(model); } }
我的问题是:如何实施第 4 步?有人有什么建议吗?
【问题讨论】:
标签: javascript jquery asp.net asp.net-mvc-3 razor