1

ASP.NET MVC4

我有业务层 DLL,它与我的 DAL 通信并检索如下类:

public class ProductionParameter
    {
        public string CompanyCode { get; set; }
        public string UnitCode { get; set; }
        public string ItemDescriptionLocal { get; set; }
        public string ItemDescriptionEnglish { get; set; }
        public string ConsumedItemDescriptionLocal { get; set; }
        public string ConsumedItemDescriptionEnglish { get; set; }
        public string LotCategory1Description { get; set; }
        public string LotCategory2Description { get; set; }
        public string LotCategory3Description { get; set; }
        public string LotCategory1Code { get; set; }
        public string LotCategory2Code { get; set; }
        public string LotCategory3Code { get; set; }
        public string LineCode { get; set; }
        public string LineCodeDisplay { get; set; }

        public List<Pallet> PalletsProduced { get; set; }

    }

我的控制器获得了上述信息,但我的视图不需要上述所有信息。

例如,假设我得到 3 个生产参数类,每个类有 20 个托盘。这意味着每个生产参数有 20 个托盘生产。

我想向我的 MVC View 显示每个生产参数的合并数据。

我如何正确地做到这一点?

标准案例:

我是否在模型中创建了一个具有视图所需信息的类,然后在@model 指令中定义这个类?

阿贾克斯:

如果我想通过 AJAX 进行此更改,会发生什么变化?我的 AJAX 调用将返回合并数据或完整数据并让 AngularJS 或 Jquery 在客户端进行合并?

4

2 回答 2

4

标准案例:

我是否在模型中创建了一个具有视图所需信息的类,然后在@model 指令中定义这个类?

是的,这正是你应该做的。您定义的视图模型仅包含视图所需的属性。然后您的控制器操作会将一个或多个域模型聚合到视图模型中并将其传递给视图。

阿贾克斯:

如果我想通过 AJAX 进行此更改,会发生什么变化?我的 AJAX 调用将返回合并数据或完整数据并让 AngularJS 或 Jquery 在客户端进行合并?

您应该始终从您的控制器操作中返回一个视图模型,该视图模型只包含所需的信息给客户端。这样,您不仅可以优化带宽和网络使用,还可以让客户的生活更加轻松。他们不再需要进行复杂的数据查询和投影,而是直接使用以视图模型的形式提供给他们的信息。

结论:您的控制器操作应始终从视图中获取和传递专门设计的视图模型。

于 2013-08-28T14:38:19.060 回答
1

您可以使用ViewModels 将业务逻辑与视图分开。AViewModel表示您希望在视图/页面上显示的数据。

假设您有一个ProductionParameterclass包含以下属性的内容:

public class ProductionParameter
{
   public string CompanyCode { get; set; }
   public string UnitCode { get; set; }
   public string ItemDescriptionLocal { get; set; }
   public string ItemDescriptionEnglish { get; set; }
   public string ConsumedItemDescriptionLocal { get; set; }
   public string ConsumedItemDescriptionEnglish { get; set; }
   public string LotCategory1Description { get; set; }
   public string LotCategory2Description { get; set; }
   public string LotCategory3Description { get; set; }
   public string LotCategory1Code { get; set; }
   public string LotCategory2Code { get; set; }
   public string LotCategory3Code { get; set; }
   public string LineCode { get; set; }
   public string LineCodeDisplay { get; set; }
   public List<Pallet> PalletsProduced { get; set; }
}

视图模型与域模型的不同之处在于,视图模型仅包含您要在视图上使用的数据(由属性表示)。

假设您只想在视图上显示以下属性,那么您的 ViewModel 可以是

 public class ProductionParameterViewModel
 {
    public string CompanyCode { get; set; }
    public string UnitCode { get; set; }
    public string ItemDescriptionLocal { get; set; }
    public string ItemDescriptionEnglish { get; set; }
    public string ConsumedItemDescriptionLocal { get; set; }
    public string ConsumedItemDescriptionEnglish { get; set; }
    public string LineCodeDisplay { get; set; }
    public List<Pallet> PalletsProduced { get; set; }
  } 

然后,在您的视图上,您​​可以按如下方式使用它:

@model MyProject.ViewModels.ProductionParameterViewModel

编辑:

如果您有两个模型,那么您可以将它们放在一个模型中,如下所示:

public class ViewModel1
{

}

public class ViewModel2
{

}

public class MyAggregateModel
{
   public ViewModel1 Model1 { get; set;}

   public ViewModel2 Model2 { get; set;}
}
于 2013-08-28T14:37:18.453 回答