asp.net-mvc-3 - ASP.Net MVC 3 ViewModel 数据注释

标签 asp.net-mvc-3 viewmodel data-annotations partial-classes

我正在使用 Entity Framework 4.1 开发一个 ASP.Net MVC 3 Web 应用程序,我对使用数据注释进行表单验证有点困惑。
我总是将 ViewModel 返回给 View,而不是传递实际对象,因为我意识到这是不好的做法。例如:

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

我的 View 可能会有这样的东西
@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>

为了验证这个 View ,我在部分类中创建了数据注释,如下所示
[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; }

然后在我的创建 Controller 中我有这个
[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 显示在 View ,而不是对象。

因此,我将 ViewModel 更改为如下所示
public class ViewModelListItem
{

    public int teamID { get; set; }

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

而且我还将我的创建 Controller 更改为这个
[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);
    }

同样,这可行,但我只是觉得第二种方法有点困惑,或者在第一种方法中效率不高。

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

最佳答案

我总是使用 View 模型和 AutoMapper帮助我简化我的域和 View 模型之间的映射。

查看模型:

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

关于asp.net-mvc-3 - ASP.Net MVC 3 ViewModel 数据注释,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9891040/

相关文章:

mvvm - ViewModel 之间的通信

android - 如何测试启动 viewModelScope 协程的 ViewModel 函数?安卓 Kotlin

.net - 每个 System.ComponentModel.DataAnnotations 属性的用途是什么?

asp.net - int 溢出时本地化为 "the value {0} is invalid"

c# - 带有 ReliableSqlConnection 的 MiniProfiler

asp.net - 如何从布局文件运行代码?

自定义类型的 MvvmCross 绑定(bind)属性到自定义 View

asp.net-mvc - Internet Explorer Mobile 和 MVC 应用程序的问题

java - 如何使用Annotation创建转换器?

c# - 验证策略