Partial views are loaded using JS, depending on the combobox value.

$.ajax({ type: "POST", url: '@Url.Action("loadPartial", "StartService", new { area = "Total" })', data: { partialName: partialName }, async: false, success: function (viewName) { }, error: function () { } }); 

Method in controller

 [HttpPost] public PartialViewResult loadPartial(string partialName) { //ищем путь к представлению и передаем его return PartialView(path); } 

The main view and the partial are strictly typed, the models are identical. However, in a partial model is not available (null reference exception)

When called using @Html.Partial("PartialName") everything works.

update: Is there any difference if you do not tick the “Create as partial view” checkbox when creating a view (in some settings this is written)

Is it possible to programmatically find out if a partial representation is strongly typed or not?

  • Check out these links: Load PartialView with using jquery Ajax? and Rendering Partial Views using ajax do something similar. Or add a little more code, views for example. Or add a model to a partial view, and in the controller, just add a comma after it to transfer it to it like this: Dynamically pass
  • All the difference in daws when creating a view will be whether Layout assigned 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 - teran
  • and on a subject you just do not transfer the model to partial. And when you output through Partial then you have a parent model used - teran

1 answer 1

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.

  • Thank. Is it possible to programmatically recognize a strictly typed representation or not? It is just not initially known whether it is worth transferring a model to a partial view or not, but it will turn out that the model will be transferred to an untyped view and an error will most likely occur - e1s
  • It seems to me that an error can arise there only if you pass on a model of another class to a strictly typed presentation. Here the big problem is that once again this model will be formed, probably pulling the database. As an alternative way - to use a certain prefix or suffix in the naming of representations. But maybe the way is, I honestly do not know - teran
  • @ e1s, by the way, here’s an option for you to define the type of view model stackoverflow.com/a/26683290/1216425 . In your case, you need a central couple of lines. - teran
  • @ e1s added answer - teran