Pregunta

When is it more appropriate to use asynchronous calls from the view to get data vs prepopulating it server-side when creating the instance of the viewmodel?

For instance, if my view has some dropdown menus that need to be loaded with values for the user. When might it be better to add List to the viewmodel vs making an ajax call on $(document).ready.

Obviously, one time it may be preferred, is when I'm not the authority on the list of items in the dropdown (i.e. making a call to another web api).

Outside of that, what is the right approach here? I feel like populating these dropdowns in the controller is too bulky.

¿Fue útil?

Solución

When you cant know in advance what the data will be and the total dataset is too large or dynamic to load and select from on the client.

You are attempting to give the user the best experience, which normally (after 'working') means 'fastest'

  1. A Single drop down with a static list.

The fastest solution is to populate the drop down server side.

  1. Two static drop downs, the second being filtered by the first selection

The fastest solution for small datasets is to populate both lists server side and do the filtering client side without an ajax call.

  1. Two static drop downs when the second is filtered by the first but the total list is large

In this case the time to download the entire list will be longer than the time to download the filtered list + ajax call

  1. Where the data changes dynamically while you are on the page.

Here we must use ajax or refresh the entire page

Licenciado bajo: CC-BY-SA con atribución
scroll top