简体   繁体   English

在ASP.NET MVC中创建包含父类属性的ViewModel

[英]Creating ViewModel that contains parent class properties in ASP.NET MVC

I am trying to understand how to create a ViewModel that contains properties from a class in my domain model as well as properties from a parent class. 我试图了解如何创建一个ViewModel,其中包含我的域模型中的类的属性以及父类的属性。

I want to have a ViewModel that contains all of the LoadSession properties and the TradingPartner Description , but I'm not sure how to map this all up in the ViewModel. 我想要一个包含所有LoadSession属性 TradingPartner Description的ViewModel,但是我不确定如何在ViewModel中将其全部映射。 Any help or advice would be greatly appreciated. 任何帮助或建议,将不胜感激。

This is my main class I'm accessing named LoadSession : 这是我正在访问的主类,名为LoadSession

public partial class LoadSession
{
    public LoadSession()
    {
        this.AcceptedTransactions = new HashSet<AcceptedTransaction>();
        this.RejectedTransactions = new HashSet<RejectedTransaction>();
    }

    public int LoadSessionId { get; set; }
    public int Import { get; set; }
    public string FilePath { get; set; }
    public string TradingPartnerBatchId { get; set; }
    public System.DateTime Started { get; set; }
    public int RecordsOnFile { get; set; }
    public int RecordsAfterGroupFilter { get; set; }
    public int RecordsAccepted { get; set; }
    public int RecordsRejected { get; set; }
    public System.DateTime Completed { get; set; }
    public bool Success { get; set; }
    public Nullable<int> Extract { get; set; }

    public virtual ICollection<AcceptedTransaction> AcceptedTransactions { get; set; }
    public virtual Extract Extract1 { get; set; }
    public virtual Import Import1 { get; set; }
    public virtual ICollection<RejectedTransaction> RejectedTransactions { get; set; }
}

The Import property is a foreign key for this Import class (Import = ImportId): Import属性是此Import类的外键(Import = ImportId):

public partial class Import
{
    public Import()
    {
        this.GroupPlans = new HashSet<GroupPlan>();
        this.ImportGroups = new HashSet<ImportGroup>();
        this.MatchingGroups = new HashSet<MatchingGroup>();
        this.LoadSessions = new HashSet<LoadSession>();
    }

    public int ImportId { get; set; }
    public string Description { get; set; }
    public int Format { get; set; }
    public int Interface { get; set; }

    public virtual Interface Interface1 { get; set; }
    public virtual Format Format1 { get; set; }
    public virtual ICollection<GroupPlan> GroupPlans { get; set; }
    public virtual ICollection<ImportGroup> ImportGroups { get; set; }
    public virtual ICollection<MatchingGroup> MatchingGroups { get; set; }
    public virtual ICollection<LoadSession> LoadSessions { get; set; }
}

The Interface property is a foreign key for this Interface class (Interface = InterfaceId): Interface属性是此Interface类的外键(Interface = InterfaceId):

public partial class Interface
{
    public Interface()
    {
        this.Extracts1 = new HashSet<Extracts1>();
        this.Imports = new HashSet<Import>();
    }

    public int InterfaceId { get; set; }
    public string Description { get; set; }
    public int TradingPartner { get; set; }

    public virtual ICollection<Extracts1> Extracts1 { get; set; }
    public virtual ICollection<Import> Imports { get; set; }
    public virtual TradingPartner TradingPartner1 { get; set; }
}

And the TradingPartner property is a foreign key for this TradingPartner class (TradingPartner = TradingPartnerId): 而且TradingPartner属性是此TradingPartner类的外键(TradingPartner = TradingPartnerId):

public partial class TradingPartner
{
    public TradingPartner()
    {
        this.Interfaces = new HashSet<Interface>();
    }

    public int TradingPartnerId { get; set; }
    public string Description { get; set; }

    public virtual ICollection<Interface> Interfaces { get; set; }
}

Well, those are all your domain objects right... 好吧,这些都是您所有的域对象...

Create a repository that takes your Domain object and transforms then to into a view model with the properties you need... 创建一个存储库,该存储库将使用您的Domain对象,然后将其转换为具有所需属性的视图模型...

I am not sure what it is your view needs, but from your statements you state that you want properties of Load session + TradingPartner.Description So create something like this... 我不确定您的视图需要什么,但是从您的陈述中可以看出,您需要Load session + TradingPartner.Description的属性。

public class LoadSessionTradingPrtNrVM
{
    public LoadSession()
    {
        this.AcceptedTransactions = new HashSet<AcceptedTransaction>();
        this.RejectedTransactions = new HashSet<RejectedTransaction>();
    }

    public int LoadSessionId { get; set; }
    public int Import { get; set; }
    public string FilePath { get; set; }
    public string TradingPartnerBatchId { get; set; }
    public System.DateTime Started { get; set; }
    public int RecordsOnFile { get; set; }
    public int RecordsAfterGroupFilter { get; set; }
    public int RecordsAccepted { get; set; }
    public int RecordsRejected { get; set; }
    public System.DateTime Completed { get; set; }
    public bool Success { get; set; }
    public Nullable<int> Extract { get; set; }
    public string Description { get; set; }

    public virtual ICollection<AcceptedTransaction> AcceptedTransactions { get; set; }
    public virtual Extract Extract1 { get; set; }
    public virtual Import Import1 { get; set; }
    public virtual ICollection<RejectedTransaction> RejectedTransactions { get; set; }
}

To get from Domain models to ViewModels you would use a repository, or some other pattern that takes what you get from your database and transforms it into what you need for your views. 要从Domain模型转换为ViewModels,您将使用存储库或其他某种模式,该模式将从数据库中获取数据并将其转换为视图所需的数据。

This is kind of raw, but the theory should hold... 这是原始的,但理论应该成立...

public class DataRepository {

      LoadSessionTradingPrtNrVM TransformToVM(LoadSession inputA, TradingPartner inputB){
            LoadSessionTradingPrtNrVM newOBJ = new LoadSessioNTradingPrtNrVM();
            newOBJ.LoadSessionId = ipnutA.LoadSessionID;
            newOBJ.Import = inputA.Import
            //Here is the property from your Transform object
            newOBJ.Description = inputB.Description
            //...  Continue to transform one object into the other... 
            //You could add as many members from as many different objects as you want into 
            //Your view model following that pattern. 
      }
}

I didn't have a chance to run this through a C# compiler, but you should get the general idea. 我没有机会通过C#编译器运行此程序,但是您应该了解一般想法。 I am sure there is a more elegant pattern that can accomplish the same thing. 我敢肯定,有一种更优雅的模式可以完成同样的事情。 But this is a decent solution off the top of my head. 但这是一个不折不扣的解决方案。

Another option is to include the domain model objects as properties in the view model. 另一种选择是将域模型对象作为属性包含在视图模型中。 For example: 例如:

// View model.
public class UserViewModel
{
    public AddressModel Address;  // Assuming "AddressModel" is a doman model.
    public string FirstName;
    public string LastName;
}

And in the view you can access the properties as: 在视图中,您可以按以下方式访问属性:

@Model.Address.AddressLine1
@Model.Address.City
// etc...

The Html helpers handle this just fine, but if you are manually naming inputs in your view don't forget to adjust those names to match. HTML助手可以很好地解决此问题,但是如果您在视图中手动命名输入,请不要忘记调整这些名称以使其匹配。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM