【问题标题】:MVC - Best/cleanest way to use data from multiple view models in a single view?MVC - 在单个视图中使用来自多个视图模型的数据的最佳/最干净的方式?
【发布时间】:2017-01-04 14:20:31
【问题描述】:

我仍在学习 MVC 的诀窍。我知道这个问题可能是 MVC 101,但我无法找出最好的解决方法: 从 2 个视图模型中获取数据并在单个视图中使用的最佳方法是什么?

我继承了一个应用程序,它通过 Views 设置了一堆电子邮件模板。这些模板都使用相同的视图模型:

@model MyCompany.Mvc.MyApplication.Models.Emails.EmailTemplateViewModel

到目前为止,这适用于所有模板,因为模板数据相当通用。实际上更多的是围绕通用电子邮件数据设置不同视图的 html 设计。

然而,现在他们想要一个打破这种模式的电子邮件模板。它仍然是一个模板并包含“EmailTemplates”模型中的数据,但它还需要包含附加信息,这些信息都已包含在单独的视图模型中:

@model CustomerViewModel

对于我的新电子邮件模板视图,从这两个模型中提取的最佳方法是什么?

所以,作为一个简单的例子,这里是我的模型:

EmailTemplateViewModel

public class EmailTemplateViewModel
{

    public string TodaysDateLongFormat
    {
        get
        {
            DateTime value = DateTime.Now;

            return value.ToString("MMMM") + " " + value.Day + ToOrdinal(value.Day) + ", " + value.Year;
        }
    }
}

客户视图模型

public class CustomerViewModel
{
     public string CustomerFirstName { get; set; }
     public string CustomerLastName { get;set; }
     public string HasMissingOrderField { get; set; }
 }

NewEmailTemplateView.cshtml(当前的多模型声明显然会失败)

 @model MyCompany.Mvc.MyApplication.Models.Emails.EmailTemplateViewModel
 @model CustomerViewModel
 <table style="border-top: 5px; padding-bottom: 5px;">
    <tr>
        <td>
            Date: @Model.TodaysDateLongFormat
        </td>
    </tr>
    <tr>
        <td>
            Customer First Name: @Model.CustomerFirstName
        </td>
    </tr>
    <tr>
        <td>
            Customer Last Name: @Model.CustomerLastName
        </td>
    </tr>
</table> 

解决这个问题的最佳方法是什么?

谢谢

【问题讨论】:

    标签: asp.net-mvc asp.net-mvc-4 model-view-controller


    【解决方案1】:

    在父视图模型下拥有多个视图模型没有错。

    public class ParentViewModel
    {
        public ChildViewModel1 ChildViewModel1 { get; set; }
        public ChildViewModel2 ChildViewModel2 { get; set; }
    }
    

    然后您可以将视图中的模型设置为@model ParentViewModel,之后您可以使用通常的 Razor 语法访问每个子视图模型(及其各自的属性):

    @Model.ChildViewModel1.Property1@Model.ChildViewModel2.Property3

    【讨论】:

      【解决方案2】:

      我认为这里最好的答案是只构建一个合适的视图模型并使用控制器填充所需的数据。

      NewEmailTemplateViewModel

      public class NewEmailTemplateViewModel
      {
           public CustomerViewModel Customer { get; set; }
           public EmailTemplateViewModel Email { get; set; }
      }
      

      NewEmailTemplateView.cshtml

      @model MyCompany.Mvc.MyApplication.Models.Emails.NewEmailTemplateViewModel
      
       <table style="border-top: 5px; padding-bottom: 5px;">
          <tr>
              <td>
                  Date: @Model.Email.TodaysDateLongFormat
              </td>
          </tr>
          <tr>
              <td>
                  Customer First Name: @Model.Customer.CustomerFirstName
              </td>
          </tr>
          <tr>
              <td>
                  Customer Last Name: @Model.Customer.CustomerLastName
              </td>
          </tr>
      </table> 
      

      在控制器中创建您的视图模型并分配属性:

      var model = new NewEmailTemplateViewModel();
      model.Customer = your customerViewModel;
      model.Email = your modelViewModel;
      return model;
      

      【讨论】:

        猜你喜欢
        • 2011-06-09
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-12-13
        • 1970-01-01
        • 1970-01-01
        • 2011-04-08
        相关资源
        最近更新 更多