Asp.net mvc 3 Net MVC 3视图模型数据注释

Asp.net mvc 3 Net MVC 3视图模型数据注释,asp.net-mvc-3,viewmodel,data-annotations,partial-classes,Asp.net Mvc 3,Viewmodel,Data Annotations,Partial Classes,我正在用EntityFramework4.1开发一个ASP.NETMVC3Web应用程序,我对使用数据注释进行表单验证感到有点困惑。 我总是将ViewModel返回到视图,而不是传递实际对象,因为我意识到这是一种糟糕的做法。例如: public class ViewModelTeam { public Team Team { get; set; } } 我的观点可能是这样的 @model UI.ViewModels.ViewModelTeam @Html.HiddenFor(

我正在用EntityFramework4.1开发一个ASP.NETMVC3Web应用程序,我对使用数据注释进行表单验证感到有点困惑。 我总是将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>
然后在我的创建控制器中,我有这个

[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,而不是对象

因此,我将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);
    }
同样,这是可行的,但我只是觉得第二种方法有点混乱,或者在第一种方法中没有那么有效


我很想听听其他人对此的看法。感谢您的帮助,我为这么长的帖子道歉。

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

视图模型:

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");
    }
}
公共类团队控制器:控制器
{
公共操作结果创建()
{
var模型=新团队视图模型();
返回视图(模型);
}
[HttpPost]
公共行动结果创建(团队视图模型)
{
如果(!ModelState.IsValid)
{
返回视图(模型);
}
团队=Mapper.Map(模型);
存储库。DoSomethingWithTeam(团队);
返回操作(“成功”);
}
}

如果我的ViewModel代表了一个对象,该对象在创建控制器中有30个属性,如果创建失败,我是否必须将每个属性重新分配给ViewModel,即ViewModel.property1=team.prop1,ViewModel.property2=team.prop2,ViewModel.property3=team.prop3。。。viewModel.property30=team.prop30等。这似乎效率低下,但AutoMapper可能就是这么做的?我以前从未用过,这很有道理。回答得很好。谢谢。谢谢达林·迪米特洛夫的分享。只是一个问题,所以您只在ViewModel上使用DataAnnoration,而不在模型上使用DataAnnoration?看看这个@GibboK,我个人根本不使用数据注释。我用它来代替。根据我正在处理的给定项目的具体情况,我可能在视图模型和域模型上都没有验证器,或者只在视图模型上有验证器。至少应该对视图模型进行验证,至于领域模型,那么业务规则将决定。感谢达林的共享,我将考虑FaltualValual.NET也为我的项目。
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");
    }
}