【问题标题】:Pass IEnumerable variable to view in ASP .NET MVC传递 IEnumerable 变量以在 ASP .NET MVC 中查看
【发布时间】:2012-09-14 15:44:27
【问题描述】:

我会说一点英语。我试着问我的问题。

我有一个模型。它的名字是 Product.cs

  Product
  {
    public int TypeId { get; set; }/*these are at the same time field of Type Table*/
    public string TypeName { get; set; }

    public int TradeMarkId { get; set; }/*at the same time field of TradeMark Table*/
    public string TradeMarkName { get; set; }

    public int ProductId { get; set; }/*at the same time field of TProduct Table*/
    public string ProductName { get; set; }
    public int TId { get; set; }
    public int TMId { get; set; }

    public List<TypeProduct> typeList { get; set; }
  }

我的控制器页面

  My controller 
  [HttpGet]
  public ActionResult TradeMarkProduckAdd()
  {
    Product product = new Product();
    TypeList typeList = new TypeList();
    product = typeList.TypeListOf(product);
    return View(product);//"This doesn't work"            
  }

它说一个类型错误 传入字典的模型项的类型为“TypeMark.Models.Product”,但此字典需要类型为“System.Collections.Generic.IEnumerable`1[TypeMark.Models.Product]”的模型项。

当我收到此错误时,我将 return View(product); 更改为 return View((IEnumerable)product); 但它没有再次工作。

查看页面

    @using TypeTradeMark.Models
    @model IEnumerable<Product>
    @using (Html.BeginForm("AddProduct", "Home", FormMethod.Post))
    {
      @foreach(var item in Model as List<Product>)
      {                              
        @item.ProductName
        @item.??//checkbox for every record      
        @item.??//dropdownlist for trademarks      
      }    
    }

类型列表类

    TypeList class

    public class TypeList
    {
      VtDataContext Vt = new VtDataContext();
      public Product TypeListOf(Product typeListOf)
      {          
        var query = (from c in Vt.Types select c);
        typeListOf.typeList=new List<Type>(query.ToList());
        return typeListof;
      }
    }

我的桌子

    Type : TypeId, TypeName
    TradeMark : TradeMarkId,TradeMarkName
    TProduct : ProductId,ProductName,TId,TMId//TId relation with TypeId, TMId relation with TradeMarkId

我无法解决问题你能帮帮我吗?谢谢

【问题讨论】:

  • 您的模型有点奇怪...您如何创建数据库?你必须支持已经存在的数据库,还是你自己创建它?
  • 我创建了数据库。有错吗?
  • 不,没有错,但我认为你的模型是错误的。如果我理解正确的话……你有这样的对象:产品、商标和产品类型。一种产品有一个商标,几种类型。例如,某些 aSer 笔记本有“aSer”商标,属于计算机技术、办公技术、技术、笔记本等产品类型。我说的对吗?
  • 是 aSer 是商标,computer 是 Type,aSer computer 是 Tproduct 表记录。我的模型可能是错误的,但我想在模型中使用我的数据库的多个表字段。我不能在模型中使用一个表。例如我不能使用@model 商标。因为在我看来,我使用商标名称和产品名称和类型名称。

标签: asp.net-mvc drop-down-menu checkbox ienumerable


【解决方案1】:

在您的视图中,您已声明@model IEnumerable,这意味着此视图被强类型化为 Product 类型的枚举,并且您的视图将始终期望一个类型为 Product 的枚举。您在这里遇到的错误是因为您传递了一个与视图类型不匹配的 Product 对象。

如果您想坚持使用IEnumeratble&lt;product&gt;,您可能需要在 Controller 或 TypeListOf() 方法中创建产品列表并将其传递给 View。

【讨论】:

    【解决方案2】:

    您查看期望的Product 类型对象列表,请参阅 (@model IEnumerable&lt;Product&gt;)。

    尝试使用这样的东西:

    return View(new List<Product> { product, product2 })
    

    【讨论】:

    • 谢谢,但有一个地方我不明白。返回视图(产品);产品已经是一个列表(我认为)不是吗?我的列表(表示产品)来自数据库。那么如何在返回视图中创建一个新列表
    • 您的视图是强类型的(您是使用@model 制作的)。如果您希望您的视图接受一种产品,只需删除 IEnumerable,请使用 @model Product
    【解决方案3】:

    我相信你需要像这样创建一个界面:

    public interface IProduct
    {
        IEnumerable<Product> Products { get; }
    }
    

    更改您的控制器以使用此 IEnumerable 接口而不是类 Product。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-12-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多