views:

583

answers:

3

I am trying to update a <div> in my view when the user clicks on an Ajax.ActionLink. However, it always navigates the entire page rather than inserting the server's response into the element I specify.

I feel like I'm doing everything in this example, but even after creating the simplest test case I still can't create the behavior I want.

In the test case that follows, I load /Company/test and after clicking on "Go!", I expected the <div> to be replaced with "All done", but instead the whole page navigates to /Company/test_ajax.

I'm sure I'm missing something here. Thanks in advance.

CompanyController

public ActionResult test()
{
    return View();
}

public ActionResult test_ajax()
{
    return Content("All done");
}

test.aspx

<%@ Page Title="" Language="C#" Inherits="System.Web.Mvc.ViewPage" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"&gt;
<html xmlns="http://www.w3.org/1999/xhtml"&gt;
<head>
<title>test</title>
<script src="../../Scripts/jquery-1.3.2.min.js" type="text/javascript"></script>
<script src="../../Scripts/MicrosoftAjax.js" type="text/javascript"></script>
<script src="../../Scripts/MicrosoftMvcAjax.js" type="text/javascript"></script>
</head>
<body>

<h2>test</h2>
<%= Ajax.ActionLink("Go!", "test_ajax",
    new AjaxOptions {
        UpdateTargetId="viewport"
        }) %>
<div id="viewport">Replace me!</div>

</body></html>
A: 

I had to modify the AjaxOptions in my ActionLink call to get it working:

<%= Ajax.ActionLink("Update", "UpdateContent", new AjaxOptions() { HttpMethod = "Post", UpdateTargetId = "target", InsertionMode = InsertionMode.Replace })%>
mannish
This did not work for me:<%= Ajax.ActionLink("Go!", "test_ajax", new AjaxOptions() { HttpMethod="Post", UpdateTargetId="viewport", InsertionMode=InsertionMode.Replace} ) %>
Stephen Jennings
A: 

I stuck at the same problem. No matter what AJAX Options I supply it always shows a blank page instead of updating the div content.

Any ideas?

Maik Koster
A: 

Your example works as expected on my computer. Check if files MicrosoftAjax.js and MicrosoftMvcAjax.js are really present in ../../Scripts folder.

Alexander Prokofyev
I'm pretty sure this was my problem. I had problems getting the right sources for scripts and styles unless I used the Url.Content("~/project/path/script.js") syntax.
Stephen Jennings