【问题标题】:pass multiple data as a list to View in MVC将多个数据作为列表传递给 MVC 中的 View
【发布时间】:2016-08-22 04:11:17
【问题描述】:

如何将 LINQ 数据传递给 View。我使用了下面的代码,但它抛出了转换错误。

namespace WebApplication1.DataContext
{
    public class Repositary
    {
        public  ProductMaster productMaster { get; set; }
        public  ProductDetail productDetail { get; set; }
    }
}`

操作方法:-

public ActionResult ProductMasterWithDetails()
{
    Model1 db=new Model1();
    Repositary rep = new Repositary();
    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 };

    ViewBag.result = varResult.ToList();

    return View();
}

错误:无法初始化类型“WebApplication1.DataContext.Repository” 使用集合初始化器,因为它没有实现 'System.Collections.IEnumerable'
C:\Users\msnsh\onedrive\documents\visual studio 2013\Projects\WebApplication1\WebApplication1\Controllers\ProductController.cs 55 141 WebApplication1

在视图中:

@foreach (var result in  ViewBag.result){

}

我有两个模型类如下。

namespace WebApplication1.DataContext
{
    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; }
    }
}

namespace WebApplication1.DataContext
{
    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; }
    }
}

如果我们有两个模型类,那么如何替换下面的单个模型命名空间?

@model IEnumerable

我无法创建如下的单个类,因为我的模型有两个类,

public Class ProductCustomModel
   {
        public int ProductId { get; set; }
        public int ProductName { get; set; }
        public int Price { get; set; }
        public int ManufactureBy { get; set; }
   }

这是我的 Model1 课程。

namespace WebApplication1.DataContext
{
    using System;
    using System.Data.Entity;
    using System.ComponentModel.DataAnnotations.Schema;
    using System.Linq;
    using System.Data.Entity.Spatial;
    public partial class Model1 : DbContext
    {
        public Model1()
            : base("name=Model1")
        {
        }

        public virtual DbSet<ProductDetail> ProductDetails { get; set; }
        public virtual DbSet<ProductMaster> ProductMasters { get; set; }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            modelBuilder.Entity<ProductDetail>()
                .Property(e => e.ManufactureBy)
                .IsUnicode(false);

            modelBuilder.Entity<ProductMaster>()
                .Property(e => e.ProductName)
                .IsUnicode(false);
        }
    }
}

【问题讨论】:

  • 创建一个包含这 4 个属性的视图模型,将查询投射到视图模型(不是匿名对象)。将该集合返回到视图(您的视图将有@model List&lt;yourModel&gt;(不要使用ViewBag
  • 能否给个示例代码怎么做?
  • 您的示例代码不清楚,Model1 类是什么,为什么您初始化了在任何地方都没有使用的rep 变量?
  • Model1 是 EntityName
  • 不要在答案中添加您的问题..!!

标签: asp.net-mvc linq view


【解决方案1】:

如果您在 varResult 中获取数据,那么您应该尝试一下,

在控制器中。

    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 }).ToList();
    return View("path to the view",varResult);

可见

    @model ModelName
    @foreach (var result in  Model){

    }

【讨论】:

  • 你的 Model1 是什么。
【解决方案2】:

您需要将ViewBag 更改为customModel/Class,这会将List 发送到您的视图。

型号:

namespace WebApplication1.DataContext
{
    public class Repositary
    {
        public  ProductMaster productMaster { get; set; }
        public  ProductDetail productDetail { get; set; }
    }

   public Class ProductCustomModel
   {
        public int ProductId { get; set; }
        public int ProductName { get; set; }
        public int Price { get; set; }
        public int ManufactureBy { get; set; }
   }
}

将您的操作方法更改为:

public ActionResult ProductMasterWithDetails()
{
    Model1 db=new Model1();
    Repositary rep = new Repositary();
    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 };

    //Make your List Here like this and add the values in it.
    List<ProductCustomModel> productCustomModelList = new List<ProductCustomModel>(); 
    Foreach(ProductCustomModel item in varResult)
    {
          ProductCustomModel singleData = new ProductCustomModel();

          ProductId = item.ProductId,
          ProductName = item.ProductName,
          Price = item.Price,
          ManufactureBy = item.ManufactureBy

          productCustomModelList.Add(singleData);
    }
    return View(productCustomModelList);

}

在视图中(.cshtml)

@model IEnumerable<WebApplication1.DataContext.ProductCustomModel>
@foreach (var result in  Model){
    //.. operations
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-07-26
    • 2019-11-28
    • 2012-05-13
    • 1970-01-01
    • 2018-12-08
    • 1970-01-01
    • 2020-02-11
    • 2014-03-10
    相关资源
    最近更新 更多