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:

  1. your model specification in partial view incorrect, it's not.

  2. you're passing in wrong thing call partial or renderpartial. 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.

  1. 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 of kid.

  2. in main view add following:

    @html.displayfor(m => m.kids) 
  3. party!


Comments

Popular posts from this blog

basic authentication with http post params android -

vb.net - Virtual Keyboard commands -

css - Firefox for ubuntu renders wrong colors -