2012-03-27 75 views
18

我正在用Entity Framework 4.1开发ASP.Net MVC 3 Web应用程序,我对使用数据注释进行表单验证感到有点困惑。 我总是返回一个ViewModel到视图,而不是传递实际的对象,因为我意识到这是不好的做法。例如: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; } 

而且我也改变了我的创建控制器添加到该

[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); 
    } 

再次,这工作,但我刚刚得到的感觉第二种方法在做这件事的第一种方式上有点麻烦或者效率不高。

我希望听到别人对此的想法。感谢您的帮助,我为这么长的帖子道歉。

回答

11

我总是使用视图模型和AutoMapper来帮助我简化我的域和视图模型之间的映射。

视图模型:

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

,然后一个常用模式:

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

如果我的视图模型来表示其中有说30个楼盘中,创建控制器内,如果创建失败的对象,然后我必须将每个属性分配回ViewModel,即viewModel.property1 = team.prop1,viewModel.property2 = team.prop2,viewModel.property3 = team.prop3 ... viewModel.property30 = team.prop30等这看起来效率不高,但也许这就是AutoMapper所做的事情?我从来没有用过它。 – tgriffiths 2012-03-27 14:06:13

+0

这很有意义。很好的答案。谢谢。 – tgriffiths 2012-03-27 14:09:37

+0

感谢Darin Dimitrov分享。只是一个问题,所以你只使用ViewModel上的DataAnnoration,而不使用Model?看看这个http://forums.asp.net/t/1502378.aspx – GibboK 2012-06-29 07:20:51