3

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?

Jason Berkan
  • 8,734
  • 7
  • 29
  • 39
Anders
  • 12,556
  • 24
  • 104
  • 151
  • It should be extremely fast locally. Are you running the website through Visual Studio or in IIS? How long does it take the first page to load? – Ryan Aug 12 '10 at 03:09
  • Are you using FF or Chrome for testing? – Yakimych Aug 12 '10 at 07:20
  • I'm running the testing in Visual Studio localhost. It takes about 3-5 seconds for the page to load. And then the action link takes about 1-2 seconds. It varies a bit though. Sometimes it's faster. But then it becomes slower again... I'm using Chrome as my test browser. – Anders Aug 12 '10 at 10:51
  • Can you check whether it is as slow in IE or Opera, or whether it is faster? – Yakimych Aug 12 '10 at 12:26
  • Just for test disable firewall and virus software. – Robert Koritnik Aug 12 '10 at 12:37

2 Answers2

3

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

Firefox and Chrome slow on localhost; known fix doesn't work on Windows 7

and here

https://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.

Community
  • 1
  • 1
Yakimych
  • 17,612
  • 7
  • 52
  • 69
  • 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 Aug 12 '10 at 17:17
0

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
  • 9,606
  • 2
  • 36
  • 50
  • 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 Aug 12 '10 at 10:58
  • 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 Aug 12 '10 at 11:07
  • 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 Aug 12 '10 at 17:19