views:

103

answers:

2

I've just started out trying MVC 2 and Ajax, and I'm wondering if I'm doing something wrong, because I was under the impression that Ajax would make changes in a webpage very fast. The example I have is with the Ajax actionlink:

<div>
  <%: Ajax.ActionLink("Dita", "AjaxView", new AjaxOptions { UpdateTargetId = "myDiv" })%>
</div>
<div id="myDiv">
    Change this text</div>

And the Action method:

public ActionResult AjaxView(string id)
{
     return Content("Text changed!"); ;

}

This is a rather short simple text string, and still it takes about 1-2 seconds before the text shows up. Maybe ajax isn't supposed to do what I thought it would, but I was thinking I could use it for instant previews of text and images sort of like a rollover function (by the way I was wondering if the actionlink can be set to invoke the action method on mouseover rather than click?)

Is it normal that it is this slow or am I missing something?

A: 

I think you've misunderstood slightly.. There is nothing about AJAX that will necessarily make your Web application faster. What AJAX does is to only load the information you need instead of loading the entire page over again. That way you can make subtle changes to the page you're viewing without having to refresh the entire page.

The point being - when you call AjaxView it still has to do a call back to the server which will take time no matter what you do. The reason why this action is slow might rely on different factors; - Your server might be busy doing something else, hence consuming resources - You just built the assembly, making the call slower the first time around

Yngve B. Nilsen
Right, but all the hype around ajax, almost the "slogan" seems to be "no more waiting on slow postbacks to the server" even though I know it still has to call back a small object to the server. But I thought the point would be it was so fast it was hardly noticeable. So if ajax can't be counted on to do that, is there another way to do this for previews of text that is instant? Would jquery offer a better way or would it be the same (the text has to come from the server anyway...)? See http://www.microsoft.com/sv/se/default.aspx as an example of the effect I mean (left menu)
Anders Svensson
You could preload the information when you load the page initially, so that the information is already in the HTML that is sent to the viewer.. This way you can easily show/hide/manipulate the HTML using a Javascript library like jQuery as you mention. Remember both Microsoft Ajax and Jquery relies on Javascript. They are just libraries that makes it easier for everyone to write clean and reliable Javascript. I'd take a look at jQuery if I were you. It changed the way I created Web applications, and it's really not that hard to get started with!
Yngve B. Nilsen
Yeah, it appears the problem was the browser support as stated above, and then it was really really fast, so it seems it works fine even if the content is on the server. Thanks anyway, and I have started to look into jquery, it seems really appealing nonetheless!
Anders Svensson
+1  A: 

It might be an IPv6 DNS resolution issue with FF and Chrome when working with localhost. Fixes described here:

http://stackoverflow.com/questions/1726585/firefox-and-chrome-slow-on-localhost-known-fix-doesnt-work-on-windows-7

and here

http://superuser.com/questions/174715/is-there-a-way-to-disable-ipv6-in-googles-chrome

I would try in IE and Opera first to check if it works faster.

Note: if that's actually the problem, this has nothing to do with AJAX.

Yakimych
Thanks! That appears to be the answer. I tried with IE, and it worked perfectly, instant refresh, no matter if I loaded a simple textstring or a partial view (BTW, when learning about Ajax, it seems loading a page without the body tag etc seems to be the suggestion for loading a piece of html, but I haven't read anything about using a partial view for this, wouldn't this be ideal? It doesn't have any of the body etc to start with... Anyway, again thanks for solving this!
Anders Svensson