简体   繁体   中英

Ajax.ActionLink in MVC3 not working

I'm developing an Asp.net MVC3 website from VS2012RC . I'm trying to create a partial view to the website using Ajax Actionlink. But it is not working. After trying everything I know with my team and a lot of searching, I then tried it on a TestProject. To my further depression that is also not working. I have tried to work around this for a long time and now I'm calling SO.

This is how I created my test project. Please let me know whether I am making a mistake or this is a fault in my machine.

  1. I have created an MVC3 empty Project with html5 semantic markup and with the view engine of Razor.
  2. Then I added a Home controller to my project and created a view for View() action.
  3. Then I created another action TestAjax().
  4. Then I Created a partial view in the Home Folder in Views Folder .

This is my Index view:

    @{
    ViewBag.Title = "Index";
    }

    <div>
    @Ajax.ActionLink("This is a test ajax","testAjax",new AjaxOptions{ UpdateTargetId =                                       "TestAjax" ,HttpMethod ="GET",InsertionMode =InsertionMode.InsertAfter})

    </div>

    <div id ="TestAjax">
    </div>

Partial View is :

<h1>Testing testing testing testing</h1>

Now what I expect is that when I click the link the header hast to appear below the link in the TestAjax division. But what actually happens is in home page the link appears. When I click the link, it will go to the "/Home/testAjax" and display only the partial view. There is no partial view.

Can you tell me what is the wrong with this procedure.

EDIT:

<head>
<meta charset="utf-8" />
<title>@ViewBag.Title</title>
<link href="@Url.Content("~/Content/Site.css")" rel="stylesheet" type="text/css" />
<script src="@Url.Content("~/Scripts/jquery-1.6.2.min.js")" type="text/javascript">         </script>
<script src="@Url.Content("~/Scripts/modernizr-2.0.6-development-only.js")" type="text/javascript"></script>
<script src="@Url.Content(" ~/Scripts/jquery.unobtrusive-ajax.js")" type = "text/javascript"> </script>  
</head>

When I check the resources in the webpage of the view Index():

<a href="/Home/TestAjax" onclick="Sys.Mvc.AsyncHyperlink.handleClick(this, new Sys.UI.DomEvent(event), { insertionMode: Sys.Mvc.InsertionMode.insertAfter, httpMethod: 'GET', updateTargetId: 'TestAjax' });">This is a test ajax</a>

Referenced the ~/Scripts/jquery.unobtrusive-ajax.js js file was not in my layout/view. Since I was not familiarized with ajax. So, people who are new to ajax in MVC and came here by Googling, First check this reference mentioned above is in your layout/view.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM