Sagar Joshi
Sagar Joshi

Reputation: 582

mvc 4 razor - best way to render HTML

I have started using MVC 4 - razor.

In that, I have found some ways to render HTML on some action.

  1. Return View in action result.
  2. Generate HTML at server side based on my data and return it as an parameter of JsonResult and append this HTML to body
  3. Return data parameter of JsonResult and create HTML at client side and append it to the body

I have tried all three ways in my application and all are working properly.

But which is the best way to use in terms of complex/heavy HTML or large data and performace?

Upvotes: 1

Views: 564

Answers (1)

shakib
shakib

Reputation: 5469

Depends on your requirement, i usually go for

solution 1, for initial view rendering with ActionResult(ViewResult) with razor view benefiting from @html extensions.

solution 2 can be sent as ContentResult as it is more "text/html" than "application/json".

solution 3 works very well for dynamic html in ajax calls, incorporation with some template engine like handlebar or jquery.tmpl which can be used to render substantial amount of html with a very thin json payload.

hope this helps.

Upvotes: 4

Related Questions