11

新MVC3,我有幾個問題,希望如果有人能回答/提供鏈接:什麼時候使用視圖模型,局部模板,模板和處理孩子的綁定與MVC 3

  1. 何時應該使用查看模型?不推薦使用域名嗎?我發現我的視圖模型是我的域對象的副本,並沒有看到值...
  2. 什麼時候應該使用部分?是否只有部分視圖會被重用?
  3. 什麼時候應該使用顯示模板和編輯器模板?我可以使用這些沒有視圖模型?
  4. 如何創建編輯屏幕,其中父對象和子對象列表都可編輯?即頂部(父)的幾個字段和下面的字段網格(如可編輯的行),特別是,我如何進行綁定?不使用automapper。

謝謝!

回答

23

什麼時候應該使用查看模型?不推薦使用域名嗎?我發現我的視圖模型是我的域對象的副本,並沒有看到該值...

應始終使用視圖模型。您不應該在視圖中使用您的域模型。

查看模型不是域模型的精確副本。他們總是有一些與視圖的具體要求有關的差異。例如,在一個屏幕上,您想要展示域模型的一些屬性,並在其他屏幕上顯示其他屬性。因此,您也將擁有不同的驗證要求,因爲一個屬性將在一個屏幕上需要,而其他屏幕上則不需要。因此,您在這些視圖模型上也會有不同的數據註釋。

什麼時候應該使用Partials?是否只有部分視圖會被重用?

不僅如果視圖將被重用。部分可以用來使你的觀點更加結構化。另外,如果您使用的是AJAX,則偏旁變得更容易。您可以將AJAX請求發送給控制器操作,該操作將返回一個局部視圖,允許您僅更新DOM的部分內容。

什麼時候應該使用顯示模板和編輯器模板?我可以使用這些沒有視圖模型?

總是。您可以將它們與任何強類型模型一起使用,但應始終使用視圖模型(請參閱前一問題的答案)。

如何創建編輯屏幕,父對象和子對象列表都可編輯?即頂部(父)的幾個字段和下面的字段網格(如可編輯的行),特別是,我如何進行綁定?不使用automapper。

那而是一如既往你開始定義您的視圖模型將代表/包含的屬性,你想提出這個屏幕上編輯回答的人一個非常寬泛的問題:

public class ChildViewModel 
{ 
    [Required] 
    public string Name { get; set; } 
} 

public class ParentViewModel 
{ 
    [Required] 
    public string Name { get; set; } 

    public IEnumerable<ChildViewModel> Children { get; set; } 
} 

然後控制器:

public class HomeController: Controller 
{ 
    public ActionResult Index() 
    { 
     // TODO: Fetch an actual domain model from your repository, 
     // and map it to the view model (AutoMapper is a great tool for the job) 

     var model = new ParentViewModel 
     { 
      Name = "parent name", 
      Children = Enumerable.Range(1, 5).Select(x => new ChildViewModel 
      { 
       Name = "child " + x 
      }) 
     }; 
     return View(model); 
    } 

    [HttpPost] 
    public ActionResult Index(ParentViewModel model) 
    { 
     if (!ModelState.IsValid) 
     { 
      return View(model); 
     } 

     // TODO: the model object here will contain the new values 
     // => user AutoMapper to map it back to a domain model 
     // and pass this domain model to the repository for processing 

     return RedirectToAction("Index"); 
    } 
} 

最後視圖:

@model ParentViewModel 
@using (Html.BeginForm()) 
{ 
    <h2>Parent</h2> 
    <div> 
     @Html.LabelFor(x => x.Name) 
     @Html.EditorFor(x => x.Name) 
     @Html.ValidationMessageFor(x => x.Name) 
    </div> 

    <h2>Children</h2> 
    <table> 
     <thead> 
      <tr> 
       <th>Child name</th> 
      </tr> 
     </thead> 
     <tbody> 
      @Html.EditorFor(x => x.Children) 
     </tbody> 
    </table> 
    <input type="submit" value="OK" /> 
} 

和最後一塊是一個孩子的編輯模板(~/Views/Home/EditorTemplates/ChildViewModel.cshtml):

@model ChildViewModel 
<tr> 
    <td> 
     @Html.LabelFor(x => x.Name) 
     @Html.EditorFor(x => x.Name) 
     @Html.ValidationMessageFor(x => x.Name) 
    </td> 
</tr> 
+0

謝謝!這真的很有幫助,只是想澄清幾點:1.模型綁定是否能夠自動綁定父母和孩子? 2.我是否需要爲EditorFor創建顯示和編輯器模板?或者是mvc足夠聰明,可以基於視圖模型爲我創建它們? 3.編輯特定視圖的視圖模型是否應包含該視圖所需的所有內容或只包含可編輯字段?有些領域可能需要只讀... – 2011-05-29 13:09:46

+0

偉大的答案Darin! – 2011-07-13 07:46:29

+0

我有關於如何處理編輯和創建視圖模型的相同問題。由於沒有退出子數據,我有一種情況,編輯需要創建期間無法填充的字段。在這種情況下你創建兩個視圖模型嗎? – PilotBob 2012-01-13 16:04:43