1

Following code adds an asp.net webforms control to my content placeholder in webforms application:

var someControl = (System.Web.UI.UserControl)LoadControl("~/serverPath/" + ControlName + ".ascx");
phContentControls.Controls.Add(someControl);

So i'm wondering is there any way i can add rendered asp.net mvc view to my control?

omg, wtf i've just asked :D

1 Answer 1

1

You could try doing something like the following:

This is an MvcUtility Class I use when I would like to render PartialViews or ChildActions in a Webforms Page, I don't think I have used it in a UserControl however.

Not sure what MVC version you are using but I know this works with MVC 3 and Razor Views.

public static class MvcUtility
{
        public static void RenderPartial(string partialViewName, object model)
        {
            // Get the HttpContext
            HttpContextBase httpContextBase = new HttpContextWrapper(HttpContext.Current);
            // Build the route data, pointing to the Some controller
            RouteData routeData = new RouteData();
            routeData.Values.Add("controller", typeof(Controller).Name);
            // Create the controller context
            ControllerContext controllerContext = new ControllerContext(new RequestContext(httpContextBase, routeData), new Controller());
            // Find the partial view
            IView view = FindPartialView(controllerContext, partialViewName);
            // create the view context and pass in the model
            ViewContext viewContext = new ViewContext(controllerContext, view, new ViewDataDictionary { Model = model }, new TempDataDictionary(), httpContextBase.Response.Output);
            // finally, render the view
            view.Render(viewContext, httpContextBase.Response.Output);
        }

        private static IView FindPartialView(ControllerContext controllerContext, string partialViewName)
        {
            // try to find the partial view
            ViewEngineResult result = ViewEngines.Engines.FindPartialView(controllerContext, partialViewName);
            if (result.View != null)
            {
                return result.View;
            }
            // wasn't found - construct error message
            StringBuilder locationsText = new StringBuilder();
            foreach (string location in result.SearchedLocations)
            {
                locationsText.AppendLine();
                locationsText.Append(location);
            }
            throw new InvalidOperationException(String.Format("Partial view {0} not found. Locations Searched: {1}", partialViewName, locationsText));
        }

        public static void RenderAction(string controllerName, string actionName, object routeValues)
        {
            RenderPartial("RenderActionUtil", new RenderActionVM() { ControllerName = controllerName, ActionName = actionName, RouteValues = routeValues });
        }
    }

To render a ChildAction you will need a partial view in the Shared MVC Views Folder:

@model YourNamespace.RenderActionVM

@{
    Html.RenderAction(Model.ActionName, Model.ControllerName, Model.RouteValues);
}

And the View Model:

public class RenderActionVM
{
    public string ControllerName { get; set; }
    public string ActionName { get; set; }
    public object RouteValues { get; set; }
}

And finally in your webforms page call like this:

<% MvcUtility.RenderPartial("_SomePartial", null); %> 

<% MvcUtility.RenderAction("SomeController", "SomeAction", new { accountID = Request.QueryString["id"], dateTime = DateTime.Now }); %>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.