c# - The model item passed into the dictionary is of type , but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable -
i'm getting following error when trying view index view:
the model item passed dictionary of type 'system.collections.generic.list1[grcwebapp.models.membershiptype]', dictionary requires model item of type 'system.collections.generic.ienumerable1[grcwebapp.viewmodels.listmembershiptypeviewmodel]'.
the controller is:
public actionresult listclubmembershiptype(listmembershiptypeviewmodel model, int clubid) { var types = s in db.membershiptypes (s.clubid == clubid) orderby s.type select s; return view(types.tolist()); }
and view model is:
public class listmembershiptypeviewmodel { [display(name = "type")] public string type { get; set; } public int clubid { get; set; } }
the view is:
@model ienumerable<grcwebapp.viewmodels.listmembershiptypeviewmodel> @{ viewbag.title = "club membership types"; } <h2>club membership types</h2> <table class="table"> <tr> <th> @html.displaynamefor(model => model.type) </th> </tr> @foreach (var item in model) { <tr> <td> @html.displayfor(modelitem => item.type) </td> </tr> } </table> @html.partial("addclubmembershiptype")
your query passing list<membershiptype>
view, view expects ienumerable<listmembershiptypeviewmodel>
change method to
public actionresult listclubmembershiptype(int clubid) { var types = s in db.membershiptypes (s.clubid == clubid) orderby s.type select s; var model = types.select(t => new listmembershiptypeviewmodel { type = t.type, clubid = clubid }); return view(model); }
side note: method should not include parameter listmembershiptypeviewmodel model
Comments
Post a Comment