【问题标题】:Map multiple Models to single ViewModel将多个模型映射到单个 ViewModel
【发布时间】:2016-08-30 14:23:52
【问题描述】:

当我有 2 个或更多模型并且我想在一个表单中使用所有这些模型的属性时,最佳做法是什么?

namespace TestApplication.Models
{
  public class Parent
  {
    public int Id { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }

    public string Street { get; set; }
    public string HouseNumber { get; set; }
    public string City { get; set; }

    public string Email { get; set; }
    public string Mobile { get; set; }
  }
}

namespace TestApplication.Models
{
  public class Student
  {
    public int Id { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public DateTime Birthday { get; set; }

    public int Grade { get; set; }
    public char Class { get; set; }

    public string Email { get; set; }
    public string Mobile { get; set; }
  }
}

=== 我需要将生成的 ViewModel 传递给 FormController 操作来处理表单。

=== 表单(cshtml 文件)本身将包含两个类的所有属性的输入字段。我还想在表单中包含 2 个以上的课程。

  1. 我应该使用 AutoMapper 并将两个/多个模型映射到一个 ViewModel 中吗? 如果是,最好的方法是什么?

  2. 我是否应该创建一个将 Student 和 Parent 类作为属性的通用 FormClass?

  3. 还有其他/更好的方法吗?

【问题讨论】:

标签: c# asp.net-mvc asp.net-core automapper asp.net-core-mvc


【解决方案1】:

为此,我建议以下解决方案。我部分同意你的第二点。

  1. 模型应该是这样的。

      public class Parent
        {
            public int Id { get; set; }
            public string FirstName { get; set; }
            public string LastName { get; set; }
    
            public string Street { get; set; }
            public string HouseNumber { get; set; }
            public string City { get; set; }
    
            public string Email { get; set; }
            public string Mobile { get; set; }
        }
    
        public class Student
        {
            public int Id { get; set; }
            public string FirstName { get; set; }
            public string LastName { get; set; }
            public DateTime Birthday { get; set; }
    
            public int Grade { get; set; }
            public char Class { get; set; }
    
            public string Email { get; set; }
            public string Mobile { get; set; }
        }
    
    public class FormClass  // This is what you suggested but it use other two model
    {
        public Student Student { get; set; }
    
        public Parent Parent { get; set; }
    }
    
  2. View 应该使用 FormClass

    @model HelloMvc.FormClass
    
     <form method="post" action="/">
     <div class="row"> 
     <div class="col-md-4">
        @Html.EditorFor(cc=>cc.Parent)
    </div>
    <div class="col-md-4">
        @Html.EditorFor(cc=>cc.Student)
    </div>
    <div class="col-md-4">
       <input type="submit" />
    </div>
    </div>
    </form>
    

注意:

  1. 控制器应该是这样的。

     public class HomeController : Controller
     {
        [HttpGet("/")]
         public IActionResult Index() 
        {
            return View();
        }
    
        [HttpPost("/")]
        public IActionResult Index(FormClass model) 
       {
        return View(model);
       }
    }
    

【讨论】:

  • 虽然不完全完美,但 dotnetstep 的答案最接近我的想法。顺便说一句,你应该使用标签助手 :) 它们比 html 助手酷十亿倍。
【解决方案2】:

最好的方法是为相似的属性使用不同的名称。例如,Email 中的 Parent 使用 ParentEmailStudent 使用 StudentEmail

【讨论】:

    【解决方案3】:

    由于您拥有表示层,为了简单和快速生产/部署,我会选择#2,而且我不必担心我发送给客户端的字段。如果这个应用程序是一个 API,我将使用 #1 并使用 Automapper 使用 Profile 将视图模型映射到您的域类。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-23
      • 1970-01-01
      • 2015-01-07
      • 2013-08-20
      相关资源
      最近更新 更多