问题
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:
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");
}
}
来源:https://stackoverflow.com/questions/9891040/asp-net-mvc-3-viewmodel-data-annotations