c# - 数据从 DAL 传输到 MVC View

标签 c# asp.net-mvc

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

    }

我的 Controller 获取以上信息,但我的 View 不需要以上所有信息。

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

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

如何正确操作?

标准案例:

我是否在模型中创建一个包含 View 所需信息的类,然后在@model 指令中定义此类?

AJAX:

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

最佳答案

Standard Case:

Do i create in Models a class with exactly the info i need for the view and then define this class in the @model directive?

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

AJAX:

What changes if i want to make this via AJAX? My AJAX call will return the consolidated data or the full data and let AngularJS or Jquery make the consolidation on the client?

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

结论:您的 Controller 操作应始终采用专门设计的 View 模型并将其传递给 View 。

关于c# - 数据从 DAL 传输到 MVC View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18490951/

相关文章:

c# - 如何检测 Lucene 索引中是否已经存储了类似的文档

c# - Mac 上 Mono 中的 DllImport 出现 DllNotFoundException

asp.net-mvc - 如何获得像 "http://stackoverflow.com/questions/1074/asp-mvc"这样的漂亮 URL

asp.net - 授权与 Active Directory .NET Core 1.1、.Net Framework 的属性交互

asp.net - 为什么即使我更改了 site.css,样式也没有改变?

c# - 在cshtml页面中处理html标签和c#脚本

c# - 加密 ASP.NET 连接字符串的正确方法是什么?

c# - 无论如何,当异常冒出时,我为什么要写 throw ?

c# - MSMQ 和 ProtoBuf-Net 序列化 C#

c# - Linq 通过 List<T> (System.Collection.Generic.List) 对象过滤 IQueryable<T> (System.Data.Linq.DataQuery) 对象?