Dejan.S
Dejan.S

Reputation: 19128

Make a partialview into html "string" in my controller, for json request

I'm making a jQuery JSON post and want to return a PartialView as HTML. In my controller I need to make the PartialView into HTML.

I found some examples, but most of them involve a BaseController. My project does not allow me to use a BaseController. I would rather need an extension that I could use in the controller instead.

I looked at some code and found an example, but I'm having some problems with using it. I don't know how to pass in the current controller I use.

[HttpPost]
public ActionResult HtmlJsonTryout(/*passing values*/)
{
    //code here......
    if (first.CartID == 0)
    {               
        var viewData = m_cartViewDataFactory.Create();
        var miniCart = ControllerExtensionsHelper.PartialViewToString("HERE I NEED TO PASS CONTROLLER RIGHT?", "_FullCart", viewData);
        var cart = PartialView("_CartSum", viewData);

        this.Response.ContentType = "application/json";
        return Json(new
        {
            Status = "OK",
            MiniCart = miniCart,
            Cart = cart
        });
    }
}

public static class ControllerExtensionsHelper
{
    public static string PartialViewToString(this Controller controller)
    {
        return controller.PartialViewToString(null, null);
    }

    public static string RenderPartialViewToString(this Controller controller, string viewName)
    {
        return controller.PartialViewToString(viewName, null);
    }

    public static string RenderPartialViewToString(this Controller controller, object model)
    {
        return controller.PartialViewToString(null, model);
    }

    public static string PartialViewToString(this Controller controller, string viewName, object model)
    {
        if (string.IsNullOrEmpty(viewName))
        {
            viewName = controller.ControllerContext.RouteData.GetRequiredString("action");
        }

        controller.ViewData.Model = model;

        using (StringWriter stringWriter = new StringWriter())
        {
            ViewEngineResult viewResult = ViewEngines.Engines.FindPartialView(controller.ControllerContext, viewName);
            ViewContext viewContext = new ViewContext(controller.ControllerContext, viewResult.View, controller.ViewData, controller.TempData, stringWriter);
            viewResult.View.Render(viewContext, stringWriter);
            return stringWriter.GetStringBuilder().ToString();
        }
    }
}

Upvotes: 2

Views: 3574

Answers (1)

Darin Dimitrov
Darin Dimitrov

Reputation: 1038830

You are not using the extension method correctly. You invoke extension methods on an instance of the class they are extending. Please read the documentation about extension methods:

[HttpPost]
public ActionResult HtmlJsonTryout(int amount)
{
    if (first.CartID == 0)
    {               
        var viewData = m_cartViewDataFactory.Create();
        string miniCart = this.PartialViewToString("_FullCart", viewData);
        string cart = this.PartialViewToString("_CartSum", viewData);

        return Json(new
        {
            Status = "OK",
            MiniCart = miniCart,
            Cart = cart
        });
    }
}

Upvotes: 2

Related Questions