Pass IEnumerable variable for viewing in ASP.NET MVC

I speak a little English. I am trying to ask my question.

I have a model. this is the name of 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 page

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

it speaks of an error of the type The model element passed to the dictionary is of type "TypeMark.Models.Product", but this dictionary requires a model element of the type "System.Collections.Generic.IEnumerable`1 [TypeMark.Models.Product]".

when i got this error i changed return View (product); return View ((IEnumerable)) , but it did not work again.

view page

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

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

my tables

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

I could not solve the problem. Can you help me? thanks

+4
source share
3 answers

You are looking at a list of pending objects of type Product , see ( @model IEnumerable<Product> ).

Try using something like this:

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

I believe that you need to create an interface like this:

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

Modify your controller to use this IEnumerable interface instead of the Product class.

0
source

In your view, you declared @model IEnumerable, which means that this view is strongly typed to enumerate the type Product, and your view will always wait for the enumeration to be typ Product. The error you get here is that you are passing a single Product object that does not match the View type.

If you want to stick with IEnumeratble<product> , you may need to create a list of products in the controller or in the TypeListOf () method and pass it to the View.

0
source

Source: https://habr.com/ru/post/1435659/


All Articles