I use this method to embed partial MVC data in web form pages. Not sure if it works in the webforms user element, but that should be possible.
Step 1. In the MVC part of your application, create the following helper function. This does all the hard work:
namespace MvcApplication { // create a dummy controller public class DummyController : Controller { } public static class MvcPartialHelper { public static void RenderPartial(string partialViewName, object model) { ControllerContext controllerContext; HttpContextBase httpContextBase; IView view; RouteData routeData; ViewContext viewContext; httpContextBase = new HttpContextWrapper(HttpContext.Current); routeData = new RouteData(); routeData.Values.Add("controller", typeof(DummyController).Name); controllerContext = new ControllerContext(new RequestContext(httpContextBase, routeData), new DummyController()); view = ViewEngines.Engines.FindPartialView(controllerContext, partialViewName).View; viewContext = new ViewContext(controllerContext, view, new ViewDataDictionary { Model = model }, new TempDataDictionary(), httpContextBase.Response.Output); view.Render(viewContext, httpContextBase.Response.Output); } } }
then on your web page (or user control):
add the following to refer to the above:
<%@ Import Namespace="MvcApplication" %>
and then when you need to display partial, you can add something like:
<% MvcPartialHelper.RenderPartial("~/views/shared/TestPartial.ascx", "hello - this is my model"); %>
where the second parameter is your "Model".
I use this technique extensively in a mixed MVC / Webforms environment and it works like a dream!
Enjoy
source share