Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.Net MVC 3 ViewModel Data Annotations

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.

like image 578
tcode Avatar asked Mar 27 '12 13:03

tcode


People also ask

Why data annotation is not working in MVC?

A data annotation is applied is to a datetime property that is bound to a mvc datetime edit control. When the data annotation is violated the validation error is not being displayed on the corresponding mvc date time control as displayed in the image.

How ModelState IsValid works in MVC?

ModelState. IsValid indicates if it was possible to bind the incoming values from the request to the model correctly and whether any explicitly specified validation rules were broken during the model binding process. In your example, the model that is being bound is of class type Encaissement .

What are data annotations in ASP.NET MVC?

We can easily add validation to our application by adding Data Annotations to our model classes. Data Annotations allow us to describe the rules we want applied to our model properties, and ASP.NET MVC will take care of enforcing them and displaying appropriate messages to our users.

What is @model in Cshtml?

The @model directive allows access to the list of movies that the controller passed to the view by using a Model object that's strongly typed. For example, in the Index.cshtml view, the code loops through the movies with a foreach statement over the strongly typed Model object: CSHTML Copy.


1 Answers

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");
    }
}
like image 142
Darin Dimitrov Avatar answered Oct 31 '22 17:10

Darin Dimitrov