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…
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:
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. 😉
Now you can simply do this:
Also note that you can modify these functions to render a View (rather than a PartialView) with this small change:
Enjoy!