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.

1 Answer 1

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");
    }
}
Sign up to request clarification or add additional context in comments.

7 Comments

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.
This makes alot of sense. Great answer. Thank you.
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 forums.asp.net/t/1502378.aspx
@GibboK, personally I don't use Data Annotations at all. I use FluentValidation.NET 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.
Thanks Darin for sharing, I will consider FluentValidation.NET too for my project.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.