0

I have a partial view in MVC 3 .NET. Every time I render the partial view, I get an error saying the Model is null. I would like for the partial view to get the data itself without passing data to it. Is it possible?

Edit @html.partial("_partialView")

In the Partial View @Model is null and if I try to access any property of @Model, it crashes since it is null. So I need that everytime I call @html.partial("_partialView"), the partial view to get the data from the server and populate @Model.

Thanks for your help guys!

Shak Ham
  • 1,209
  • 2
  • 9
  • 27
  • It's completely possible. But can you provide your code so we can help your specific problem? – Steven V Jul 22 '13 at 13:38
  • Possibly related: http://stackoverflow.com/questions/6426381/mvc3-pass-model-to-partial-view?rq=1 – Mgetz Jul 22 '13 at 13:39

2 Answers2

0

It is possible if you don't use model data inside of view, but if you do - feel free to provide model :)

Sergio
  • 6,900
  • 5
  • 31
  • 55
0

Check if you have @model SomeModelType line somewhere at the beginning of your view and also all calls to @Model.SomeProperty (they are the actual cause of exception). Remove all this stuff if you don't want to use model inside your partial view.

  • I do want to use the model. But it is null since I'm not passing data to the partial view, I want it to get it itself. – Shak Ham Jul 22 '13 at 14:14
  • How do you want it to get the model? If you have not passed a model in partial view, it will be null inside it and you can't do anything because `WebViewPage.Model` property is read-only (may be you can make some customizations in ASP.NET MVC infrastructure, but I don't think that it's worth effort). You can supply your model to the view in another way, not via `WebViewPage.Model` property, but I don't think it will be good design. – Oleksandr Kobylianskyi Jul 24 '13 at 20:21