-1

I know jQuery is currently everybody's darling hammer (truth be told, I'm enamored with it, too), but I'm wondering if dynamically generating html might be more elegantly, or more easily, done using Razor/C#. For example, to update a page, consider this Razor pseudocode:

@{
   var divContent = functions.getDivContent("Platypus");

   if(IsPost)
   {
      divContent = functions.getDivContent(request[mammalName]);
   }
   else
   {
      ;//anything?
   }
}

//The getDivContent() function in functions (not shown) returns dynamically generated html

HTML pseudocode:

<body>
<form method="POST" action="" >
  <input type="Select" name="mammalName" ... />
</form>
  <div>@divContent</div>
</body>

jQuery pseudocode:

$('mammalName').selectionChanged() {
    $('form').submit();
});

Doing all of this in a jQuery function, such as using json data and calling .getJson(), would obviate the need for a complete page refresh, but in either case the server has to be queried for data, so I don't know if that's really such an advantage, especially in the case (mine) where the part of the page being refreshed is the lion's share of the page.

Is there a flaw in my thinking/design? Is there a compelling reason to choose jQuery over Razor here?

4

1 回答 1

1

Yes, there are definitely things to consider when choosing to change pages using ajax or to change pages using a whole page reload.

performance-wise, using ajax will always come out on top because you're only requesting the entire page once, and then using ajax to request partials.

As far as maintenance, you don't actually need to change anything server-side to use ajax rather than Razor. The only added maintenance will be coding your client-side code to properly handle the fact that the page isn't going to reload.

If you used Ajax, you could optimize your requests FOR ajax, making your pages return only the needed content rather than the entire page, further improving performance. This comes at a slight cost to maintenance because it's adds another layer of complexity, however, if done properly, is easy to maintain.

UX wise, ajax will generally be better due to not having to reload the page. There is a loss of back-button functionality unless you also code that into your client-side ( also not very difficult ).

If you add SEO into the equation, things get a bit more difficult unless you coded your site FIRST to work without ajax, then added ajax functionality on top of it that way the crawlers can still access the entire site without ajax.

于 2013-06-18T16:12:41.200 回答