5

i have an asp.net mvc view that with the help of jquery ajax updates a div. Should i use a controller that return a PartialView or json? And the performances?

1

7 Answers 7

11

This ... question ... has ... been ... asked ... before :)

Sign up to request clarification or add additional context in comments.

Comments

3

I'd lean towards JSON myself. The reason is that JSON affords you the flexibility to bring back different data. e.g.

You might bring back 100 results, AND some metadata about what those results are (keywords, grouping, etc.) AND info about how many more results are available, how fresh the results are, paging info...

Likewise other un-related info might come back - status messages, "new mail!" indicators etc.

With pure HTML returned you limit further options to do anything other than update the HTML content of a container.

Comments

2

If you want the user be able to switch between different views, and this is data that can simply be included on page load, then just include onload then hide/show or load as json and then populate as needed.

If this is a very simple request that needs to be updated as needed and you don't plan on other similar needs in your app then just return html.

However if you are making a request that submits data or performs some action, I am a fan of returning a consistent json response with error boolean, error message and content/data elements.

Comments

2

To me, the coolest way of doing AJAX is returning slim data packets, not pre-rendered HTML. So I would say go with the JSON option...

... But it all depends of your needs. It is much easier to show pre-rendered HTML snippets, so if you need something fast a partial view may be a better option.

But all this has been discussed before. So take a look at this link: The AJAX response: XML, HTML, or JSON?. The author presents a good, platform independent summary of the 3 main AJAX response options, with advantages and disadvantages of each one.

Comments

1

Returning HTML most probably requires more server-side processing because the server will have to do the HTML formatting. Returning JSON will require more client-side processing because the client will have to format the HTML. Performance is thus a matter of where you'd like the processing to take place. If your server is under high load, you might want to offload some of the processing to the clients.

Comments

1

If you are compressing the response then return HTML otherwise return JSON and build the HTML client side. In an ideal world your server should always compress the response but you may have a poorly configured server that is not under your control. The point of my answer however is to keep it simple because on the server side the difference between building HTML and JSON is quite minimal. Also building HTML on the client can be a bit slow for users with outdated machines. (Or browsers... you know the browser I am talking about...)

Note: My answer should be understood in the most general context. Without looking at your technology stack and target market it would be wrong of me to answer in any other way. Remember that most rules in programming are more like guidelines.

3 Comments

Either approach should be compressed/delivered via gzip...so that shouldn't be a factor.
Why? What's the specific reason you suggest this way?
ah, I see what you're saying now.
0

It depends on what you are trying to achieve. If you are creating a "thick" client (i.e. the server just processes data and the browser handles the rest), then the server should respond back w/ JSON.

It sounds like for your case, the page is already loaded. Considering this, JSON is the way to go and then have your JS handle the processing of the UI. There are no major performance trade-offs from using either method, but the best practice IMO would be to use JSON.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.