asp.net mvc - Nested collection view partial in ASP MVC -
i'm trying pass kids
collection main view partial. main view typed , kids
attribute has_many
association mainmodel
.
i have default details view @model myapp.models.mainmodel
.
inside view, want show children,
@html.partial("_kidsview", model.kids)
which configured in mainmodel.cs
:
public icollection<kid> kids { get; set; }
and specified in parital _kidsview
such:
@model icollection<myapp.models.kid>
i getting invalidoperationexception
says
the model passed dictionary if of type 'system.data.entity.dyanmicproxies.mainmodel_#whole_bunch_of_numbers' dictionary requires model item of type 'system.collections.genereic.icollection[myapp.models.kid]
i have tried not using partial , calling foreach
loop directly on @foreach (var item in model.kids)
hasn't worked either.
how can properley pass in?
i feel may doing wrong can't see it.
edit:
ok, solution isn't working. figure it's in 1 of models that's not right.
here kid.cs:
[column("main_model_id")] public int mainmodelid { get; set; } public virtual mainmodel mainmodel {get; set; }
and mainmodel.cs
public icollection<kid> kids { get; set; }
the error you're getting explicit , can caused 2 things:
your model specification in partial view incorrect, it's not.
you're passing in wrong thing call
partial
orrenderpartial
. specifically, if not specify model argument, it's filled main view's model default (which seems happening based on error, call have in question correct).
since seems neither of case, can assume there's kind of caching going on or other "stickiness" causing current code not code that's running. stop , restart debugging, , maybe close visual studio , try again, after confirm code indeed correct.
an alternative solution use display template instead - it's pretty partial view, little cleaner in situations this.
in "views" folder, create new folder called "displaytemplates". in folder, create partial view called "kid.cshtml". strongly-type view
kid
model (not enumerable,kid
). add html should rendered 1 instance ofkid
.in main view add following:
@html.displayfor(m => m.kids)
party!
Comments
Post a Comment