asp.net mvc - WCF MVC model for A view -


im trying display data view foreach loop have problem define @model can me please.

this service method

public ienumerable<categorytype> getcatlist()         {             shopentities context = new shopentities();              list<category> produkty = context.category.tolist<category>();              return changetypee(produkty);         }          private list<categorytype> changetypee(list<category> categorie)         {             list<categorytype> producttypes = new list<categorytype>(); ;             categorytype product = new categorytype();             foreach (var c in categorie)             {                 product.id = c.id;                 product.name = c.name;                 producttypes.add(product);             }              return producttypes;         } 

this contract

[operationcontract()]         ienumerable<categorytype> getcatlist(); 

and controller method

public actionresult index()         {             servicereference1.service1client proxy = new servicereference1.service1client();              return view(proxy.getcatlist());         } 

error message

compiler error message: cs1579: foreach statement cannot operate on variables of type 'system.web.mvc.viewpage<system.collections.generic.ienumerable<shop.data.categorytype>>' because 'system.web.mvc.viewpage<system.collections.generic.ienumerable<shop.data.categorytype>>' not contain public definition 'getenumerator' 

i assume error in view? sounds doing:

@foreach (var item in this) { ... } 

instead of

@foreach (var m in this.model) { ... } 

the reason error says viewpage<t> not have getenumerator, doesn't - not enumerable.

edit

actually problem, not quite described. using:

@model viewpage<ienumerable<mvc4webapp_internetapp.controllers.categorytype>>  

therefore model of type viewmodel<t>, instead need:

@model ienumerable<mvc4webapp_internetapp.controllers.categorytype> 

edit 2

  1. you'll need use @html.displayfor( m => item.name); instead of displayformodel.
  2. your changetypee method messed up

you need categorytype product = new categorytype(); inside loop, not outside, or return list every entry has value of last entry:

// don't here: categorytype product = new categorytype(); foreach (var c in categorie) {     // here     categorytype product = new categorytype();     product.id = c.id;     product.name = c.name;     producttypes.add(product); } 

Comments

Popular posts from this blog

SPSS keyboard combination alters encoding -

Add new record to the table by click on the button in Microsoft Access -

CSS3 Transition to highlight new elements created in JQuery -