c# - Nested ViewModels / Partial View problems in MVC -
c# - Nested ViewModels / Partial View problems in MVC -
i have 2 views: partial view, , view encapsulates partial view using @html.renderpartial("_partialview")
. each has own viewmodel:
public class partialviewmodel { // properties, etc. } public class mainviewmodel { public partialviewmodel p { get; set; } // properties, etc. }
i'm getting dictionary errors when load sec view (the 1 uses mainviewmodel), because view , partial view encapsulates using 2 different viewmodels. can't have them utilize same viewmodel, because partial view rendered within many other different views.
to clear, both of these views contain forms, partial view representing of shared fields between forms. given this, have options, or attempting not fit within mvc design constraints?
you going want design little differently . main view have model - lets phone call mainmodel
, , partial view can have model - we'll phone call partialmodel
public class partialmodel { /// props } public class mainviewmodel { public partialmodel partial { get; set; } // properties, etc. // per comments in other reply may want public mainviewmodel() { partial = new partialmodel(); } }
then main view have
@model mainviewmodel
then in middle of main view have like
@html.renderpartial("mypartialview", model.partial)
when render partial view can pass in model it, if designed model partial needs accessable main view's model
c# asp.net-mvc mvvm
Comments
Post a Comment