Skip to content

Instantly share code, notes, and snippets.

@SimonRice
Created December 28, 2011 15:22
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save SimonRice/1528317 to your computer and use it in GitHub Desktop.
Save SimonRice/1528317 to your computer and use it in GitHub Desktop.
Utility class to render MVC Partials & Actions in Webforms (or server side controls)
<viewdata ControllerName="string" />
<viewdata ActionName="string" />
<viewdata RouteValues="object" />
# Html.RenderAction(ActionName, ControllerName, RouteValues);
using System.IO;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
/* Sources:
*
* http://stackoverflow.com/questions/702746/how-to-include-a-partial-view-inside-a-webform/1074061#1074061
* http://www.blog.clicktricity.com/post/Using-MVC-RenderAction-within-a-Webform.aspx
* */
class WebFormController : Controller { }
public static class WebFormMVCUtil
{
static void PartialToTextWriter(TextWriter tw, string partialName, object model)
{
//get a wrapper for the legacy WebForm context
HttpContextWrapper httpCtx = new HttpContextWrapper(HttpContext.Current);
//create a mock route that points to the empty controller
RouteData rt = new RouteData();
rt.Values.Add("controller", "WebFormController");
//create a controller context for the route and http context
ControllerContext ctx = new ControllerContext(new RequestContext(httpCtx, rt), new WebFormController());
//find the partial view using the viewengine
IView view = ViewEngines.Engines.FindPartialView(ctx, partialName).View;
//create a view context and assign the model
ViewContext vctx = new ViewContext(ctx, view, new ViewDataDictionary { Model = model }, new TempDataDictionary(), tw);
//ERROR OCCURS ON THIS LINE
view.Render(vctx, tw);
}
public static void RenderPartial(string partialName, object model)
{
PartialToTextWriter(HttpContext.Current.Response.Output, partialName, model);
}
public static void RenderPartial(string partialName)
{
RenderPartial(partialName, null);
}
public static string Partial(string partialName, object model)
{
StringWriter sw = new StringWriter();
PartialToTextWriter(sw, partialName, model);
return sw.ToString();
}
public static string Partial(string partialName)
{
return Partial(partialName, null);
}
public static void RenderAction(string controllerName, string actionName, object routeValues)
{
// Requires a RenderActionPartial partial view model in the shared folder of the MVC app
RenderPartial("RenderActionPartial", new { ControllerName = controllerName, ActionName = actionName, RouteValues = routeValues });
}
public static string Action(string controllerName, string actionName, object routeValues)
{
// Requires a RenderActionPartial partial view model in the shared folder of the MVC app
return Partial("RenderActionPartial", new { ControllerName = controllerName, ActionName = actionName, RouteValues = routeValues });
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment