1

I have a project on asp.net mvc.

In some situations, when user do something, a part of html page should be updated. Whole page shouldn't be reloaded. For example, user clicks on the product category and a html markup with new products should be loaded.

What is the best practice - to use partial view and Ajax.ActionLink to update partial view products, or use $.ajax for receiving info about products and then generate html markup in javascript?

Or maybe the best practice is something else?

1 Answer 1

2

It depends on what your requirements are. In general, as you say, there are two methods:

Either load the formatted HTML through Ajax, or load a JSON and then create the HTML yourself. Each has its advantages and disadvantages:

Loading HTML

Advantages:

  • You can do generation on the server-side, thereby keeping the client-side code clean

Disadvantages

  • The size of the packet you send over the wire will be bigger
  • The request is very specific to that method alone, and it will be difficult to reuse

Loading JSON

Advantages:

  • Payload is as small as can be
  • Since you're not tying it to one implementation, you can reuse the JSON for other purposes (a mobile client for example)

Disadvantages:

  • Generation of HTML has to be done on the client-side. If you're doing this manually or with jQuery, this can be cumbersome and tends to grow the client-side script. (a solution would be to use a templating engine such as moustache.js or handlebars.js)
  • Your rendering logic is on the server AND on the client, which means not everything is in one place

As you can see, both have there pros and cons. There's no real hard rule here, and it depends on your situation.

In general, my advice would be if you want something quick and you don't think you will need the same method elsewhere use dynamic HTML loading. If you think you might want to reuse the data on other pages or with other clients, use JSON with a templating engine.

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

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.