Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Christian Bennich 11 posts 31 karma points
    Jan 13, 2012 @ 09:33
    Christian Bennich
    0

    Dynamic loading of partials from controller called by JQuery in Umbraco 5

    Hi

    I am in the process of moving an Intranet from Umbraco 4.x to Umbraco 5 - MVC 3.

    My old solution is built on a whole bunch of Jquery and Ajax loading.
    All the data loading in the old solution is done through .ashx handlers that are beeing called by Jquery, to return JSON objects.

    I have proted all my old handlers to controllers, and are down to one problematic devil :-D

    In the old solution I have a handler, that are able to take the name/path of a usercontrol as input, and then load this control, and return the HTML, to be injected into a Jquery popup window.

    The code for rendering looks like this:

            private static UserControl LoadControl(string UserControlPath, params object[] constructorParameters)

            {

                Page pageHolder = new Page();

                List<Type> constParamTypes = new List<Type>();

                foreach (object constParam in constructorParameters)

                {

                    constParamTypes.Add(constParam.GetType());

                }

                UserControl ctl = pageHolder.LoadControl(UserControlPath) as UserControl;

                // Find the relevant constructor

                ConstructorInfo constructor = ctl.GetType().BaseType.GetConstructor(constParamTypes.ToArray());

                //And then call the relevant constructor

                if (constructor == null)

                {

                    throw new MemberAccessException("The requested constructor was not found on : " + ctl.GetType().BaseType.ToString());

                }

                else

                {

                    constructor.Invoke(ctl, constructorParameters);

                }

                // Finally return the fully initialized UC

                return ctl;

            }

    It work very well in Umbraco 4.x.

    In Umbraco 5 I am trying to do something similar with Partial Views.....BUT GRRRRRRR - what ever I do I get an unhandled Exception like this:

    I am trying to do the following:

     public PartialViewResult AddCompany()

            {

                string ReturnContent = RenderPartialViewToString("AddCompany", null);

                return PartialView(ReturnContent);

            }

     

            protected string RenderPartialViewToString(string viewName, object model)

            {

                if (string.IsNullOrEmpty(viewName))
                    viewName = ControllerContext.RouteData.GetRequiredString("action");

                ViewData.Model = model;

                using (StringWriter sw = new StringWriter())

                {

                    ViewEngineResult viewResult = ViewEngines.Engines.FindPartialView(ControllerContext, viewName);

                    ViewContext viewContext = new ViewContext(ControllerContext, viewResult.View, ViewData, TempData, sw);

                    viewResult.View.Render(viewContext, sw);

                    return sw.GetStringBuilder().ToString();

                }

            }

    AddCompany is just a static test partial view.

    Can someone please enlighten me on this issue???

    I am kind of stuck here.

    /Christian

  • Christian Bennich 11 posts 31 karma points
    Jan 13, 2012 @ 09:39
    Christian Bennich
    0

    Forgot to mention - the code is in a custom assembly, and the views are palced in a folder corresponding with the name of the Controller.

  • This forum is in read-only mode while we transition to the new forum.

    You can continue this topic on the new forum by tapping the "Continue discussion" link below.

Please Sign in or register to post replies