Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How To Insert Partial views Dynamically in Asp.Net MVC

I am migrating a Webforms site to MVC. In my webforms site I have pages that utilise various combinations of User Controls, then chunks of html then Labels, Textboxes etc.

I don't want to hardwire each page so I am going to drive the output for each page from a CMS which specifies the order to insert the controls into the page.

I imagine each control will now be a Partial View in MVC. (Let me know if that's not correct).

So if I have two different partial views, ViewA and ViewB, how do I create a controller method that inserts the partial views into the view that is returned in the order determined by the CMS for a given url?

So assuming the controller method is called Reports and it takes a parameter called product.

eg //MySite/Reports?product=A returns a view containing ViewA, ViewA, ViewB, ViewA

whereas

//MySite/Reports?product=B returns a view containing ViewA, ViewB, ViewA, ViewB etc

So what should the code be for the controller method?

I hope that makes sense

like image 647
Frank Cannon Avatar asked Oct 01 '22 16:10

Frank Cannon


1 Answers

If I understood you correctly this should solve your problem

Just create a new class derived from PartialViewResult which accepts multiple view names to render them. And to make it a little more usable create a new extension method for the controller to call your customized ViewResult.

That worked for me. You can use it so simply:

public ActionResult Index()
{
    return this.ArrayView(new string[] { "ViewA", "ViewB" });
}

To make it work ArrayViewResult class should be:

public class ArrayViewResult : PartialViewResult
{
    public IEnumerable<string> Views;

    protected override ViewEngineResult FindView(ControllerContext context)
    {
        return base.FindView(context);
    }
    public override void ExecuteResult(ControllerContext context)
    {
        if (context == null)
            throw new ArgumentNullException("context");
        if (!Views.Any())
            throw new Exception("no view...");


        TextWriter writer = context.HttpContext.Response.Output;

        foreach(var view in Views)
        {
            this.ViewName = view;
            ViewEngineResult result = FindView(context);

            ViewContext viewContext = new ViewContext(context, result.View, ViewData, TempData, writer);
            result.View.Render(viewContext, writer);

            result.ViewEngine.ReleaseView(context, result.View);
        }
    }
}

Extension method:

namespace System.Web.Mvc
{
    public static class ArrayViewResultExtension
    {
        public static ArrayViewResult ArrayView(this Controller controller, string[] views)
        {
            return ArrayView(controller, views, null);
        }
        public static ArrayViewResult ArrayView(this Controller controller, string[] views, object model)
        {
            if (model != null)
            {
                controller.ViewData.Model = model;
            }

            return new ArrayViewResult
            {
                ViewName = "",
                ViewData = controller.ViewData,
                TempData = controller.TempData,
                ViewEngineCollection = controller.ViewEngineCollection,
                Views = views
            };
        }
    }
}
like image 81
Yves Avatar answered Oct 05 '22 13:10

Yves