5

I want to return back html inside my json object however this does not seem to work, my code:

return new JsonResult() { Data = new { Error = false, NewComment = PartialView("Review/InlineCommentUC", dto) } }; 

I want NewComment to have some html inside it...

What I recieve (using firebug) for the NewComment object in json format is:

TempData = [] View = null, ViewData = [] ViewEngineCollection = some data.. ViewName = name of view 

I am using Jquery to render the output onto the html, the reason for sending back a json object is, so I can handle my errors very easily.

Ideally a custom Action Result is what I am looking for...

5
  • why would you want to do so? Maybe there is an alternative solution to your problem?CommentedNov 22, 2011 at 13:05
  • @TomasJansson: why not do this? In my opinion whilst I have worked with ajax request, server side and client side - this is the most simple solution when handling errors, all I will return is: return new JsonResult(){ Data = new { Error = true, Msg= errMsg.ToString() }}; where errMsg is tied to my domain code, this is where I handle all my exceptions...
    – Haroon
    CommentedNov 22, 2011 at 13:10
  • @TomasJansson - if I am correct, even Github do this too
    – Haroon
    CommentedNov 22, 2011 at 13:10
  • just to separate your view from your implementation more I would recommend to return a "clean" json object with properties and then parse that on the client. Parsing shouldn't be that hard if you use jQuery template or such.CommentedNov 22, 2011 at 13:19
  • @TomasJansson: Totally agree with you, however I do not have a dependency on jquery templates, I may decide to do this later and go heavily onto the templating but cannot do that now, hence the solution above is the only way forward for now. Thanks for the advice anyway...
    – Haroon
    CommentedNov 22, 2011 at 13:59

2 Answers 2

6

Is http://craftycodeblog.com/2010/05/15/asp-net-mvc-render-partial-view-to-string/ what you're looking for?

Copied using the WayBackMachine.

I have run into a situation where I would like to render a partial view to a string and then return it as part of a JSON response like so:

return Json(new { statusCode = 1, statusMessage = "The person has been added!", personHtml = PartialView("Person", person) }); 

The ability to do something like this would open up a ton of amazing possibilities, so I really scoured the internet looking for a solution. Unfortunately, no one seems to have come up with a clean solution for it, so I dug into the MVC code and came up one…and because I’m such a nice guy, you get to copy it for free. ;)

public abstract class MyBaseController : Controller { protected string RenderPartialViewToString() { return RenderPartialViewToString(null, null); } protected string RenderPartialViewToString(string viewName) { return RenderPartialViewToString(viewName, null); } protected string RenderPartialViewToString(object model) { return RenderPartialViewToString(null, model); } 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(); } } } 

Now you can simply do this:

public class MyController : MyBaseController { public ActionResult CreatePerson(Person p) { if (ModelState.IsValid) { try { PersonRepository.Create(p); return Json(new { statusCode = 1, statusMessage = "The person has been added!", personHtml = RenderPartialViewToString("Person", p) }); } catch (Exception ex) { return Json(new { statusCode = 0, statusMessage = "Error: " + ex.Message }); } } else return Json(new { statusCode = 0, statusMessage = "Invalid data!" }); } } 

Also note that you can modify these functions to render a View (rather than a PartialView) with this small change:

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

Enjoy!

2
  • Thanks... I was hoping to create a custom Action Result, for now I have decided against it though... Do you know how bad the performance can be with this?
    – Haroon
    CommentedNov 22, 2011 at 13:58
  • @MotoWilliams I've grabbed the original content and re-added it to my answer.CommentedAug 26, 2013 at 19:47
1

PartialView returns a PartialViewResultdocs and that is what gets encoded to JSON.

Look at http://forums.asp.net/post/3761391.aspx on how to render the PartialView to a string

    Start asking to get answers

    Find the answer to your question by asking.

    Ask question

    Explore related questions

    See similar questions with these tags.