Get the Current ViewContext in ASP.Net MVC

Cyril Gupta picture Cyril Gupta · Jan 7, 2010 · Viewed 12.5k times · Source

I have a ASP.Net MVC JsonResult function in which I want to return the contents of a PartialView (The content has to be loaded using Ajax, and for some reason I can't return a PartialViewResult).

To render the PartialView I need the ViewContext object.

How do you get the current ViewContext object within an Action method? I don't even see HttpContext.Current in my action method.

I am using ASP.net MVC 1.

Answer

Darin Dimitrov picture Darin Dimitrov · Jan 7, 2010

a ViewContext is not available within the action method because it is constructed later before rendering the view. I would suggest you using MVCContrib's BlockRenderer to render the contents of a partial view into a string.