2012-03-27 27 views
18

Entity Framework 4.1でASP.Net MVC 3 Webアプリケーションを開発していますが、フォームの検証にData Annotationsを使用することについてちょっと混乱しています。 実際のオブジェクトを渡すのではなく、ViewModelをViewに返すのは、これが貧弱な方法であることを認識しているからです。たとえば:ASP.Net MVC 3 ViewModelデータ注釈

public class ViewModelTeam 
{ 
    public Team Team { get; set; } 
} 

マイビューは、このビューを検証するには、この

@model UI.ViewModels.ViewModelTeam 

    @Html.HiddenFor(model => model.Team.teamID) 


    <div class="editor-label"> 
     @Html.LabelFor(model => model.Team.description) 
    </div> 
    <div class="editor-field"> 
     @Html.EditorFor(model => model.Team.description) 
     @Html.ValidationMessageFor(model => model.Team.description) 
    </div> 

のようなものを持っているかもしれませんが、私はそのように

[MetadataType(typeof(TeamMetaData))] 
public partial class Team 
{ 
    public class TeamMetaData 
    { 
     [DisplayName("Team Name")] 
     [Required(ErrorMessage = "Please enter a Team Name")] 
     public object description { get; set; } 

など部分クラスでのデータ注釈を作成しました私が作成したコントローラで私はこれを持っています

[HttpPost] 
    public ActionResult Create(Team team) 
    { 
     if (ModelState.IsValid) 
     { 
      //Add team and redirect 
     } 

      //Got this far then errors have happened 
      //Add Model State Errors 


     ViewModelTeam viewModel = new ViewModelTeam 
     { 
      Team = team    
     }; 

     return View(viewModel); 
    } 

これはうまくいきますが、ViewModelsと検証に関する情報が増えれば増えるほど、ViewModelは検証されるはずです。なぜなら、その日の終わりには、ViewModelが表示されているからですビューではなく、オブジェクトです。

したがって、私は次のよう

public class ViewModelListItem 
{ 

    public int teamID { get; set; } 

    [DisplayName("Item Name")] 
    [Required(ErrorMessage = "Please enter a Team Name")] 
    public string description { get; set; } 

のように見えるために私のViewModelを変更し、私も再びこの

[HttpPost] 
    public ActionResult Create(Team team) 
    { 
     if (ModelState.IsValid) 
     { 
      //Add team and redirect 
     } 

      //Got this far then errors have happened 
      //Add Model State Errors 

     ViewModelTeam viewModel = new ViewModelTeam(); 
    viewModel.description = team.description; 

     return View(viewModel); 
    } 

に私の作成コントローラーを変更し、これは動作しますが、私は感覚を得ます2番目の方法はこれを行う最初の方法ではちょっと混乱しているか、効率的ではありません。

私はこれに関する他の人の考えを聞くことに興味があります。ご協力いただきありがとうございます。私はこのような長い投稿をお詫び申し上げます。

答えて

11

ドメインモデルとビューモデルの間のマッピングを簡単にするために、常にビューモデルとAutoMapperを使用します。

ビューモデル:その後、

public class TeamViewModel 
{ 
    [DisplayName("Team Name")] 
    [Required(ErrorMessage = "Please enter a Team Name")] 
    public string Description { get; set; } 
} 

と一般的に使用されるパターン:作成が失敗した場合、私のViewModelは、作成コントローラの内部で、30個の特性を言わなかったオブジェクトを表す場合はどう

public class TeamsController: Controller 
{ 
    public ActionResult Create() 
    { 
     var model = new TeamViewModel(); 
     return View(model); 
    } 

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

     Team team = Mapper.Map<TeamViewModel, Team>(model); 
     Repository.DoSomethingWithTeam(team); 

     return RedirectToAction("Success"); 
    } 
} 
+0

、それぞれのプロパティをViewModelに割り当てる必要がありますか?つまり、viewModel.property1 = team.prop1、viewModel.property2 = team.prop2、viewModel.property3 = team.prop3 ... viewModel.property30 = team.prop30などです。これは非効率的だと思われますが、AutoMapperは何をしているのでしょうか?私はかつてこれを使ったことがない。 – tgriffiths

+0

これは意味があります。素晴らしい答え。ありがとうございました。 – tgriffiths

+0

共有のためのDarin Dimitrovありがとうございます。ちょうど質問ですので、ViewModelではDataAnnorationのみを使用し、モデルでは使用しないでください。これを見てくださいhttp://forums.asp.net/t/1502378.aspx – GibboK

関連する問題