Model does not pass to partial view.
You don’t pass it in principle, because you are using the method signature
Controller.PartialView (String)
Creates a PartialViewResult object that renders a partial view using the specified view name.
Thus, you do not pass the data model at all, and it remains null . At the same time, you write that the model is the same in partial and normal presentation. That is, it still has to be. From here you also receive natural null reference exception . For the same reason, @Html.Partial("PartialName") works when the view uses the parent data model ( they are the same ).
Your method should use the PartialView(string, object) signature PartialView(string, object)
[HttpPost] public PartialViewResult loadPartial(string partialName) { var model = new MyViewModel(); model.data = ....; return PartialView(path, model); }
on additional issues:
- The "Partial presentation" checkbox only affects the installation of the
layout in the presentation code. - The strict typing of a view only indicates a particular @Model class at the beginning of the view. At the output, you get either a
WebViewPage<MyViewModel> or a WebViewPage<dynamic>
a small addition about the definition of typing (code, of course, not verified, a link in the comments). Probably something like that
[HttpPost] public PartialViewResult loadPartial(string partialName) { var path = // ... + partialName; Type type = BuildManager.GetCompiledType(path); var modelProperty = type.GetProperties().FirstOrDefault(p => p.Name == "Model"); if (modelProperty == null || modelProperty.PropertyType != typeof(MyViewModel)) return PartialView(path) ; // модель не используется var model = new MyViewModel(); model.data = ....; return PartialView(path, model); }
technically, this solution probably gives an answer to the question, but perhaps it is worth changing the logic somewhat, since This is not exactly normal behavior for an application at a glance.
Layoutassigned in the view code. and why recognize the typed representation? Here either the class of the model is explicitly stated in the presentation, or not, that’s the whole difference - teranPartialthen you have a parent model used - teran