views:

118

answers:

3

We've got old ASP.NET Forms pages and new MVC views and partials views in the same solution. Some pages on the site are MVC, and legacy pages are Forms.

One of these legacy Forms pages is an .ascx control. Is there any way for me to insert an MVC partial view (.ascx) into this Forms .ascx control?

A: 

No, there isn't as you don't have the Html helper needed to perform this insertion:

<%= Html.RenderPartial("foo") %>

Also your MVC partial is strongly typed (isn't it) and you won't have access to the model.

Also when you are migrating a legacy webforms application to ASP.NET MVC it should be the other way round.

Darin Dimitrov
Can't I just include the namespace of the HtmlHelper?
foobarbarfoo
`Html` is a property of `System.Web.ViewPage`, so the namespace inclusion *trick* is only when you wrote custom helpers that extent HtmlHelper, but this suppose that you already have a reference to it which is not the case in a class web form.
Darin Dimitrov
A: 

Technically it is possible, though you need to jump through some hoops to achieve what you are asking. You need to create a dummy controller context, view Context and related environment, and then create a property in your page behind code to simulate the model.

Let me know if you would like detailed instructions / example

Clicktricity
Yes, do you have a link you can point me to? Pretty please :)
foobarbarfoo
A: 

I use this technique to embed MVC partials into webforms pages. Not sure if it works in a webforms user control, but it should be possible.

Step 1. Within 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, within your web page (or user control):

add the following to reference the above:

<%@ Import Namespace="MvcApplication" %>

and then when you need to display the 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

Clicktricity