Art Of Programming

musings by Dmytrii Nagirniak

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
RenderPartial(“MyPartial”, item)

you better write
RenderPartial(“MuPartial”, new ViewDataDictionary { Model = item })

Just a quick note. Thanks to this forum post.


Dmytrii Nagirniak
Thanks. Good suggestion.
Tahir Hassan

since the ViewDataDictionary also contains the ModelState which will tell you if fields are invalid etc, it is better to contruct the ViewDataDictionary passing the current ViewData in; therefore your second argument would be as follows:

new ViewDataDictionary(ViewData) { Model = item }