0

I have a ASP.NET MVC4 controller function that returns dynamic html using a StringBuilder as a Json result.

This is an quality issue, because I don't want dynamic HTML being returned as a string property in a JSON result.

My goal is to make use of ASP.NET MVC to use .cshtml for example with a viewmodel so that I make an instance of the viewmodel, passing in the results and in the end, return the raw HTML of the view as a JSON result.

How can this be achieved ?

Patrick Peters
  • 9,456
  • 7
  • 57
  • 106
  • I'm not sure you understand what JSON is. Returning raw HTML as JSON would mean returning a single string in a JSON object. – Pluc Apr 02 '15 at 13:51
  • [Render a View as a String](http://stackoverflow.com/questions/483091/render-a-view-as-a-string?rq=1) and add it to JSON. Very simple. – Erik Philips Apr 02 '15 at 13:53

1 Answers1

2

Instead of returning an HTML string inside of a JSON object, you may want to consider an action that returns a PartialView directly.

If you can't do this, because you need to return MULTIPLE html strings in one call, then I would advise taking a look at this thread which has a nice static method to turn a rendered view into a String: Render a view as a string.

Community
  • 1
  • 1
Zack Z.
  • 238
  • 3
  • 10