ASP.Net MVC AJAX link not working

旧巷老猫 提交于 2019-12-08 01:45:17

问题


I have this link, in an effort to implement AJAX log on, on my page:

<%= Ajax.ActionLink("Log On", "LogOn", "Account", new AjaxOptions {
    UpdateTargetId = "lll",
    Confirm = "blah"}) %>

There's a div with id lll nearby. When I click the link, I get the blah confirmation (just added for debugging purposes, the behavior without it it's the same) but then nothing happens. No request ever reaches the server (because I have a breakpoint on the LogOn action method). That is in Chrome and IE8. In FF3 it opens the logon view but as a page, it doesn't download it through AJAX.

Any ideas what might be wrong?


回答1:


The problem was that the ajax action link makes a POST request by default and it was being directed to the other LogOn method (the one accepting POST) and that's why I wasn't hitting the breakpoint Also, it was failing because the necessary POST data was not being sent. Adding HttpMethod to the action link fixed it:

<%= Ajax.ActionLink("Log On", "LogOn", "Account", new AjaxOptions {
    UpdateTargetId = "lll",
    Confirm = "blah",
    HttpMethod = "Get"}) %>



回答2:


It sounds like there is some javascript error on the page causing the javascript inserted to handle the AJAX request not to fire. Have you looked at it in FireBug on page load to see if all of your Javascript loading correctly? Also, do your versions of the Microsoft javascript libraries match the version of MVC that you are using? I remember at least once in the progression of versions that I had to manually update my versions of the Microsoft javascript libraries in my project. If your project has existed through multiple versions of MVC, I'd suggest tracking down the new libraries (create a new project and copy them to your old one or open the project archive in the install directory and extract them by hand) and installing them.




回答3:


Did you include ajax client scripts in the head section:

  • MicrosoftMvcAjax.js
  • MicrosoftAjax.js


来源:https://stackoverflow.com/questions/934745/asp-net-mvc-ajax-link-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!