18

I am developing an ASP.Net MVC 3 Web application with Entity Framework 4.1 and I am getting a bit confused with regards using Data Annotations for form validation. I always return a ViewModel to a View as opposed to passing the actual object as I realise this is poor practice. For example:

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

My View might then have something like this

@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>

To validate this View, I have created Data Annotations in a partial class like so

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

And then in my create Controller I have this

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

Now, this works fine, however, the more I read about ViewModels and validation, the more it seems that it is the ViewModel that should be validated, because at the end of the day, it is the ViewModel that is being displayed in the View, not the object.

Therefore, I changed my ViewModel to look like the following

public class ViewModelListItem
{

    public int teamID { get; set; }

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

And I also changed my create Controller to this

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

Again, this works, but I just get the feeling the 2nd method is a bit messy or not as efficient at the first way of doing this.

I would be interested to hear other people’s thoughts on this. Thank you for your help and I apologise for such a long post.

tcode
  • 5,055
  • 19
  • 65
  • 124

1 Answers1

12

I always use view models and AutoMapper to help me simplify the mapping between my domain and view models.

view model:

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

and then a commonly used pattern:

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");
    }
}
Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • What if my ViewModel represented an Object which had say 30 properties, inside the create Controller, if the create fails, would I then have to assign each property back to the ViewModel, ie, viewModel.property1 = team.prop1, viewModel.property2 = team.prop2, viewModel.property3 = team.prop3 ... viewModel.property30 = team.prop30 etc. This seems inefficient, but maybe thats what AutoMapper does? I've never used it before. – tcode Mar 27 '12 at 14:06
  • This makes alot of sense. Great answer. Thank you. – tcode Mar 27 '12 at 14:09
  • Thanks Darin Dimitrov for sharing. Just a question, so you use only DataAnnoration on you ViewModel and never on the Model? Have a look at this http://forums.asp.net/t/1502378.aspx – GibboK Jun 29 '12 at 07:20
  • 1
    @GibboK, personally I don't use Data Annotations at all. I use [FluentValidation.NET](http://fluentvalidation.codeplex.com/) instead. And depending on the specific of the given project I am working on I might have validators no both the view models and the domain models or only on the view models. There should at least be validation on the view model, and as far as the domain model is concerned, then business rules will dictate. – Darin Dimitrov Jun 29 '12 at 07:38
  • Thanks Darin for sharing, I will consider FluentValidation.NET too for my project. – GibboK Jun 30 '12 at 09:30
  • if you Annotate your models directly you've created a coupling between your Models and your UI. I think I'm saying that right. That's why Darin uses the ViewModel for that purpose. – Doug Chamberlain Mar 29 '13 at 19:03
  • I meant display attributes annotations. – Doug Chamberlain Mar 29 '13 at 19:20