簡體   English   中英

在ASP.NET MVC中創建包含父類屬性的ViewModel

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

我試圖了解如何創建一個ViewModel,其中包含我的域模型中的類的屬性以及父類的屬性。

我想要一個包含所有LoadSession屬性 TradingPartner Description的ViewModel,但是我不確定如何在ViewModel中將其全部映射。 任何幫助或建議,將不勝感激。

這是我正在訪問的主類,名為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; }
}

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

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

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

好吧,這些都是您所有的域對象...

創建一個存儲庫,該存儲庫將使用您的Domain對象,然后將其轉換為具有所需屬性的視圖模型...

我不確定您的視圖需要什么,但是從您的陳述中可以看出,您需要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; }
}

要從Domain模型轉換為ViewModels,您將使用存儲庫或其他某種模式,該模式將從數據庫中獲取數據並將其轉換為視圖所需的數據。

這是原始的,但理論應該成立...

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

我沒有機會通過C#編譯器運行此程序,但是您應該了解一般想法。 我敢肯定,有一種更優雅的模式可以完成同樣的事情。 但這是一個不折不扣的解決方案。

另一種選擇是將域模型對象作為屬性包含在視圖模型中。 例如:

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

在視圖中,您可以按以下方式訪問屬性:

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

HTML助手可以很好地解決此問題,但是如果您在視圖中手動命名輸入,請不要忘記調整這些名稱以使其匹配。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM