views:

667

answers:

2

Hi all, I have the following code:

public ActionResult SomeAction()
{
    return new JsonpResult
    {
        Data = new { Widget = "some partial html for the widget" }
    };
}

I'd like to modify it so that I could have

public ActionResult SomeAction()
{
    // will render HTML that I can pass to the JSONP result to return.
    var partial = RenderPartial(viewModel); 
    return new JsonpResult
    {
        Data = new { Widget = partial }
    };
}

is this possible? Could somebody explain how?

note, I edited the question before posting the solution.

+1  A: 

This is a slightly modified version of an answer that works:

public static string RenderPartialToString(string controlName, object viewData)
        {
            ViewPage viewPage = new ViewPage() { ViewContext = new ViewContext() };

            viewPage.ViewData = new ViewDataDictionary(viewData);
            viewPage.Controls.Add(viewPage.LoadControl(controlName));

            StringBuilder sb = new StringBuilder();
            using (StringWriter sw = new StringWriter(sb))
            {
                using (HtmlTextWriter tw = new HtmlTextWriter(sw))
                {
                    viewPage.RenderControl(tw);
                }
            }

            return sb.ToString();
        }

Usage:

string ret = RenderPartialToString("~/Views/MyController/MyPartial.ascx", model);
DaveDev
This is working well for me. However, if I add a RenderPartial to the partial view I pass to this method, I start getting an exception on the RenderControl call: "Value cannot be null. Parameter name: view"
Pete Nelson
+1  A: 

Dave,

a variation on the same theme (mvc v1.0):

        protected static string RenderPartialToString(Controller controller, string partialName, object model)
    {
        var vd = new ViewDataDictionary(controller.ViewData);
        var vp = new ViewPage
        {
            ViewData = vd,
            ViewContext = new ViewContext(),
            Url = new UrlHelper(controller.ControllerContext.RequestContext)
        };

        ViewEngineResult result = ViewEngines
                                  .Engines
                                  .FindPartialView(controller.ControllerContext, partialName);

        if (result.View == null)
        {
            throw new InvalidOperationException(
            string.Format("The partial view '{0}' could not be found", partialName));
        }
        var partialPath = ((WebFormView)result.View).ViewPath;

        vp.ViewData.Model = model;

        Control control = vp.LoadControl(partialPath);
        vp.Controls.Add(control);

        var sb = new StringBuilder();

        using (var sw = new StringWriter(sb))
        {
            using (var tw = new HtmlTextWriter(sw))
            {
                vp.RenderControl(tw);
            }
        }
        return sb.ToString();
    }

usage within controller:

        public string GetLocationHighlites()
    {
        IBlockData model = WebPagesMapper.GetLocationHighlites();
        // **this** being the controoler instance
        // LocationPartial.ascx can be defined in shared or in view folder
        return RenderPartialToString(**this**,"LocationPartial", model);
    }
jim