ASP.NET MVC RenderPartial With Null Model

If you’ll try to pass null to the RenderPartial method it will actually pull the model from the page and will, most likely, give an error.
So instead of

you better write

That’s how the RenderPartial method works (I know should have been documented, blogged about, etc…, me too I find this a little strange). If you do not specify a model or pass null it will use the model of the parent page. To avoid this you might use the null coalescing operator:

And if you are really curious as to how this is implemented there’s an excerpt from the relevant parts of the ASP.NET MVC 2 source code:


Notice how the case of null model is handled.