2014-03-04 23 views
0

我們正在編寫一個ASP.NET MVC3應用程序,並希望通過使用引導和模式對話框來實現它「豐富」。ASP.NET MVC3:編輯視圖模式對話框

我現在想知道如何實現模態對話框,而不會打破ASP.NET(ModelErrors,...)中所有酷炫的工作人員。

的工作流程應該是這樣的:

  1. 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 
    ) 
    
  2. 模態對話框(簡單的div病急亂投醫與CSS)呈現編輯視圖(從控制器actionmethode返回)

    [HttpGet] 
    public ActionResult Edit(int id) { 
        // Load Data and create Model 
        var model = new ... 
        return PartialView(model); 
    } 
    
  3. 在編輯視圖的形式可用於編輯的項目,包括客戶端驗證

    @{ 
        AjaxOptions ajaxOptions = new AjaxOptions() { 
          HttpMethod = "Post", OnSuccess="modal.hideModalDiv()" 
        }; 
    } 
    @using (Ajax.BeginForm("Edit"), ajaxOptions){ 
    
    
        ... element to edit item ... 
    
        <input type="submit" value="submit" /> 
    } 
    
  4. 當提交編輯控制器梅索德識別錯誤(不通過客戶端驗證捕獲)的頁面應該再次顯示模型錯誤。 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步?有人有建議嗎?

回答

0

這種嘗試,

[HttpPost] 
public ActionResult Edit(Guid id, ItemModel model) 
{ 
    if(model != null && ModelState.IsValid) 
    { 
    return RedirectToAction("Index") 
    } 
    else 
    { 
    return PartialView(model); 
    } 

} 
+0

你好,感謝你的答案。我想你沒有得到我的問題。您的建議看起來(除了ModelState.IsValid)完全和我的一樣。我的問題是,如何處理不同類型的響應(PartialViewResult將div與Redirectresult替換爲重定向或替換另一個div)? – Tobias

0

我想你已經解決了這個問題,或發現周圍的工作,但如果你沒有,我懷疑,如果你要返回包含的JavaScript做局部視圖重定向你可以得到你描述的行爲。

例如:

RedirectToIndex.cshtml

@{ Layout = null; } 
<script type="text/javascript"> 
    window.location.href = "@Url.Action("Index")"; 
</script> 

然後更新您的行動來回報這個局部視圖。

[HttpPost] 
public PartialViewResult Edit(Guid id, ItemModel model) { 
try{ 
     //Save Item ... 
     return PartialView("RedirectToIndex") 
    } catch (Exception ex){ 
     ModelState.AddModelError("", "An error occured"); 
     return PartialView(model); 
    } 
} 

不是最優美的,但應該工作,我說應該,因爲我沒有測試過這...