加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

asp.net-mvc – 传递IEnumerable变量以在ASP.NET MVC中查看

发布时间:2020-12-16 09:37:32 所属栏目:asp.Net 来源:网络整理
导读:我会说一点点英语.我试着问我的问题. 我有一个模特.它的名字是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
我会说一点点英语.我试着问我的问题.

我有一个模特.它的名字是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]’的模型项.

当我收到此错误时,我更改了返回View(产品);至
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类

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

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

解决方法

您可以查看产品类型对象的期望列表,请参阅(@model IEnumerable< Product>).

尝试使用这样的东西:

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读