0

I am working on asp.net MVC 2 application. I have ajax.action link but it is not working. I have this code in my view:

<%@ Page Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage" %>

<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
   <script type="text/javascript">
       function success(result) {
           alert(result);
           // TODO: do something with the object
       }
   </script>
</asp:Content>

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
    <%: Ajax.ActionLink(
    "Delete", 
    "Delete", 
    new { Id = 55 }, 
    new AjaxOptions { OnComplete = "success" })
%>
</asp:Content>

and this is controller code:

 public ActionResult Index()
 {
      ViewData["Message"] = "Welcome to ASP.NET MVC!";

      return View();
 }



 public JsonResult Delete(Int32 Id) {

     return Json("Record deleted!", JsonRequestBehavior.AllowGet);
 }

But when I click the link, It shows Record deleted! in browser instead of showing as alert. Am I missing some file(s) ?

AliRıza Adıyahşi
  • 15,658
  • 24
  • 115
  • 197
DotnetSparrow
  • 27,428
  • 62
  • 183
  • 316

1 Answers1

0

You might need to include the MicrosoftAjax.js and MicrosoftMvcAjax.js scripts to your page:

<script type="text/javascript" src="<%= Url.Content("~/scripts/MicrosoftAjax.js") %>"></script>
<script type="text/javascript" src="<%= Url.Content("~/scripts/MicrosoftMvcAjax.js") %>"></script>

In ASP.NET MVC 3 and later those files are obsolete and replaced by the jQuery unobtrusive scripts.

Now that you have sent me your sample project by mail it is clear what the problem is. You have defined your success javascript function inside the <title> tag of your page which obviously cannot work.

So inside your MasterPage you should define a special placeholder for your scripts (notice also how I have fixed your hardcoded links to the CSS file with a url helper):

<head runat="server">
    <title><asp:ContentPlaceHolder ID="TitleContent" runat="server" /></title>
    <link href="<%= Url.Content("~/Content/Site.css") %>" rel="stylesheet" type="text/css" />
    <script type="text/javascript" src="<%= Url.Content("~/scripts/MicrosoftAjax.js") %>"></script>
    <script type="text/javascript" src="<%= Url.Content("~/scripts/MicrosoftMvcAjax.js") %>"></script>
    <asp:ContentPlaceHolder ID="Scripts" runat="server" />
</head>

that you could override in your view:

<%@ Page Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage" %>

<asp:Content ID="Content1" ContentPlaceHolderID="Scripts" runat="server">
    <script type="text/javascript">
        function success() {
           alert('success');
           // TODO: do something with the object
        }
    </script>
</asp:Content>

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
    <%= Ajax.ActionLink(
        "Delete", 
        "Delete", 
        new { id = 55 }, 
        new AjaxOptions { OnComplete = "success" }
    ) %>
</asp:Content>
Community
  • 1
  • 1
Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928