我們正在編寫一個ASP.NET MVC3應用程序,並希望通過使用引導和模式對話框來實現它「豐富」。ASP.NET MVC3:編輯視圖模式對話框
我現在想知道如何實現模態對話框,而不會打破ASP.NET(ModelErrors,...)中所有酷炫的工作人員。
的工作流程應該是這樣的:
IndexView與項目清單,與ActionLink的每個項目,顯示一個模式對話框
@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 )
模態對話框(簡單的div病急亂投醫與CSS)呈現編輯視圖(從控制器actionmethode返回)
[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" /> }
當提交編輯控制器梅索德識別錯誤(不通過客戶端驗證捕獲)的頁面應該再次顯示模型錯誤。 Otherwize應顯示索引頁面或刷新表格並關閉模式對話框。
[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步?有人有建議嗎?
你好,感謝你的答案。我想你沒有得到我的問題。您的建議看起來(除了ModelState.IsValid)完全和我的一樣。我的問題是,如何處理不同類型的響應(PartialViewResult將div與Redirectresult替換爲重定向或替換另一個div)? – Tobias