【问题标题】:Join two Model classes result and display it on View-MVC加入两个模型类结果并在 View-MVC 上显示
【发布时间】:2016-08-22 20:17:35
【问题描述】:

我想使用 Linq 组合以下两个类的结果并将结果显示在视图上?

using System;
    using System.Collections.Generic;
    using System.ComponentModel.DataAnnotations;
    using System.ComponentModel.DataAnnotations.Schema;
    using System.Data.Entity.Spatial;

    [Table("ProductMaster")]
    public partial class ProductMaster
    {
        public ProductMaster()
        {
            ProductDetails = new HashSet<ProductDetail>();
        }

        [Key]
        [DatabaseGenerated(DatabaseGeneratedOption.None)]
        public int ProductId { get; set; }

        [StringLength(50)]
        public string ProductName { get; set; }

        public virtual ICollection<ProductDetail> ProductDetails { get; set; }
    }


using System;
    using System.Collections.Generic;
    using System.ComponentModel.DataAnnotations;
    using System.ComponentModel.DataAnnotations.Schema;
    using System.Data.Entity.Spatial;

    public partial class ProductDetail
    {
        public int? Price { get; set; }

        [StringLength(50)]
        public string ManufactureBy { get; set; }


        public int? ProductId { get; set; }

        [Key]
        public int ProdDetailsId { get; set; }

        public virtual ProductMaster ProductMaster { get; set; }
    }

使用 LINQ 查询连接两个表数据。

 public ActionResult BindProductMasterData()
        {
           Model1 db = new Model1();
            var varResult = from pm in db.ProductMasters join pd in db.ProductDetails on pm.ProductId equals pd.ProductId select new { pm.ProductId, pm.ProductName, pd.Price, pd.ManufactureBy };
            return View(varResult.ToList());
        }

在视图中显示下表列数据 ProductId , ProductName, Price, ManufactureBy

【问题讨论】:

    标签: asp.net-mvc linq viewmodel


    【解决方案1】:

    为此视图创建一个视图模型,并将您的 LINQ 连接查询结果分配给该列表并将其传递给视图。

    public class ProductVm
    {
      public int Id { set;get;}
      public string Name  { set;get;}
      public List<ProductDetailVm> Details { set;get;}
    }
    public class ProductDetailVm
    {
       public int Id { set;get;}
       public int? Price { get; set; }
       public string ManufactureBy { get; set; }
    }
    

    在您的操作方法中,将 LINQ 查询的结果投影到 ProductVm 对象列表。

    public ActionResult BindProductMasterData()
    {
       Model1 db = new Model1();
       var varResult = db.ProductMasters
                         .Select( f =>
                    new ProductVm
                    {
                        Id = f.ProductId, 
                        Name = f.ProductName,
                        Details = f.ProductDetails.Select(g => new ProductDetailVm
                                                     { 
                                                       Id = g.ProdDetailsId ,
                                                       Price = g.Price,
                                                       ManufactureBy = g.ManufactureBy
                                                     }
                                           ).ToList()
                    }).ToList();
      return View(varResult);
    }
    

    现在确保您的视图被强类型化到我们的 ProductVm 视图模型列表中

    @model List<ProductVm>
    @foreach(var p in Model)
    {
      <h4>@p.ProductName</p>
      <p>Details</p>
      @foreach(var d in p.Details)
      {
        <p>@d.ManufacturedBy</p>
        <p>@d.Price</p>
      }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-10-28
      • 2016-04-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-09-05
      • 1970-01-01
      • 2021-10-24
      相关资源
      最近更新 更多