2011-01-10 48 views
5

我有一個ASP.NET MVC 3(剃刀)的網站,以及(簡化)模型稱爲評論ASP.NET MVC的 - 問題與EditorTemplate對ICollection的<T>映射到枚舉

public class Review 
{ 
    public int ReviewId { get; set; } 
    public bool RecommendationOne 
    { 
     // hook property - gets/set values in the ICollection 
    } 
    public bool RecommendationTwo { // etc } 
    public ICollection<Recommendation> Recommendations { get; set; } 
} 

建議如下:

public class Recommendation 
{ 
    public byte RecommendationTypeId 
} 

我也有一個枚舉稱爲RecommendationType,我用它來映射上面的建議。 (基於RecommendationTypeId)。

所以總結 - 一個單一審查具有許多建議,並且每個這些建議的映射到特定枚舉型,i暴露鉤特性,以簡化模型綁定/代碼。

所以,到查看:

@Html.EditorFor(model => model.Recommendations, "Recommendations") 

很簡單。

現在,對於編輯模板,我想顯示每個可能的RecommendationType(ENUM)複選框,如果模型具有這一建議(例如在編輯視圖),我選中此複選框。

下面是我有:

@model IEnumerable<xxxx.DomainModel.Core.Posts.Recommendation> 
@using xxxx.DomainModel.Core.Posts; 

@{ 
    Layout = null; 
} 

<table> 
    @foreach (var rec in Enum.GetValues(typeof(RecommendationType)).Cast<RecommendationType>()) 
    { 
     <tr> 
      <td> 
       @* If review contains this recommendation, check the box *@ 
       @if (Model != null && Model.Any(x => x.RecommendationTypeId == (byte)rec)) 
       { 
        @* How do i create a (checked) checkbox here? *@ 
       } 
       else 
       { 
        @* How do i created a checkbox here? *@ 
       } 

       @rec.ToDescription() 
      </td> 
     </tr> 
    } 
</table> 

由於意見建議 - 我不知道如何使用@Html.CheckBoxFor。通常這需要一個基於模型的表達式,但是我很確定如何根據當前循環枚舉值綁定到hook屬性。 E.g它需要動態做@Html.CheckBoxFor(x => x.RecommendationOne)@Html.CheckBoxFor(x => x.RecommendationTwo)

目前的解決方案我有(工作),涉及手動構建<input>(包括隱藏字段)。

但是,由於我只是在編輯器模板的懸掛,希望有經驗的人可以指向我在「強類型」的方向。

還是有更好的方法(HTML助手)我可以做到這一點?

回答

10

我會通過引入適當的視圖模型爲場景開始:

public enum RecommendationType { One, Two, Three } 

public class ReviewViewModel 
{ 
    public IEnumerable<RecommendationViewModel> Recommendations { get; set; } 
} 

public class RecommendationViewModel 
{ 
    public RecommendationType RecommendationType { get; set; } 
    public bool IsChecked { get; set; } 
} 

然後,控制器:

public class HomeController : Controller 
{ 
    public ActionResult Index() 
    { 
     // TODO: query the repository to fetch your model 
     // and use AutoMapper to map between it and the 
     // corresponding view model so that you have a true/false 
     // for each enum value 
     var model = new ReviewViewModel 
     { 
      Recommendations = new[] 
      { 
       new RecommendationViewModel { 
        RecommendationType = RecommendationType.One, 
        IsChecked = false 
       }, 
       new RecommendationViewModel { 
        RecommendationType = RecommendationType.Two, 
        IsChecked = true 
       }, 
       new RecommendationViewModel { 
        RecommendationType = RecommendationType.Three, 
        IsChecked = true 
       }, 
      } 
     }; 
     return View(model); 
    } 

    [HttpPost] 
    public ActionResult Index(ReviewViewModel model) 
    { 
     // Here you will get for each enum value the corresponding 
     // checked value 
     // TODO: Use AutoMapper to map back to your model and persist 
     // using a repository 
     return RedirectToAction("Success"); 
    } 
} 

和相應的視圖(~/Views/Home/Index.cshtml):

@model YourAppName.Models.ReviewViewModel 

@{ 
    ViewBag.Title = "Index"; 
} 

@using (Html.BeginForm()) 
{ 
    @Html.EditorFor(model => model.Recommendations) 
    <input type="submit" value="Go" /> 
} 

最後是編輯模板(~/Views/Home/EditorTemplates/RecommendationViewModel.cshtml

@model YourAppName.Models.RecommendationViewModel 
<div> 
    @Html.HiddenFor(x => x.RecommendationType) 
    @Model.RecommendationType 
    @Html.CheckBoxFor(x => x.IsChecked) 
</div> 

現在視圖代碼被清理,因爲它應該。沒有ifs,沒有循環,沒有LINQ,沒有反射,這是控制器/映射器層的責任。因此,每當您發現自己在視圖中編寫一些高級C#邏輯時,我都會建議您重新考慮視圖模型並根據需要對其進行調整。這就是視圖模型的目的:儘可能接近視圖邏輯。

+2

嚴重達林,你真棒。 :)這看起來不錯 - 我會給你一個去,回到你身邊。 +1現在。 :) – RPM1984 2011-01-11 08:14:41