c# - Loop over ModelView in razor -


i have created razor forms @foreach loop on model , produce rows. trying to same thing on viewmodel, , following error.

unable create constant value of type 'myproject.models.supportcontact'.  primitive types or enumeration types supported in context. 

my @model being passed in iqueryable(myproject.models.supportcontactformview), , supportcontact part of viewmodel, can't seem reference in model.

i can this:

@foreach (var item in model ) {      @html.displayfor(modelitem => item.supportcontact.contact) 

but error above when try process cshtml. have tried do:

@foreach (myproject.models.supportcontact item in model ) {} 

but can't drill down thru model supportcontacts. how can supportcontact class inside viewmodel?

thanks

edit:

here viewmodel declaration:

public class supportcontactformview {     public supportcontact supportcontact { get; private set; }     public selectlist priorities { get; private set; }     public selectlist contacttypes { get; private set; }     public string group_code { get; private set; } } 

in case, create viewmodel view , set so

viewmodel class

public class indexviewmodel {     public ilist<supportcontact > supportcontacts { get; set; } } 

controller

public actionresult index() {     var viewmodel = new indexviewmodel();     viewmodel.supportcontacts = iqueryablelistofcontacts.tolist();      return view(viewmodel) } 

view

@model indexviewmodel @foreach (var contact in model.supportcontacts) {      @html.displayfor(_ => contact.contact) 

you need play around bit code, doubt have property names/action method name correct first time, hope basic idea. create viewmodel class view file makes use of, , dump contact details list. make easier you.

edit:

actually, think got wrong end of stick here. error "unable create constant value of type 'myproject.models.supportcontact'. primitive types or enumeration types supported in context." means there wrong linq data.

are using entity framework or linq2sql? ignore answer viewmodel appraoch, actual issue linq related. try iterating on list of contacts in actionmethod instead e.g.

public actionmethod index() {     foreach (var contact in whateveryourcontactslistis) {     }      // ....     } 

if still have problem here, it's linq data @ fault.


Comments

Popular posts from this blog

javascript - JS causing window size to be bigger than necessary - Dropdown bug -

php - Calling a template part from a post -

How to mention the localhost in android -