Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ajax.BeginForm doesn't call onSuccess

In ASP.NET MVC 3 application I use Ajax.BeginForm to post writed text to controller.

@using (Ajax.BeginForm("Post", "Forum", new {threadId = Model.Thread.Id  }, new AjaxOptions { OnSuccess = "PostReply" }))
{
    <div id="reply-area">
        <h3 style="border-bottom:1px solid black">POST REPLY</h3>

        <span id="post-error" class="error-message"></span>
        <textarea rows="1" cols="1" id="post-textarea" name="Content">    </textarea>

         <input type="submit" class="button" value="Submit"/>

     </div>
}

In controller I have

 [HttpPost]
 public ActionResult Post(int threadId,PostModel model)
 {
     bool Success = false;
     if (ModelState.IsValid)
     {
        Success=Unit.ForumFacade.CreatePost(Unit.ForumFacade.GetThreadByID(threadId), model.Content,  CurrentUserId);
        if (Success == true) return View("PostSuccess");
    }

    return Json("fsdfds");
}

And in javascript I have this

function PostReply(isRequestSuccessed) {
    alert("asdasd");
    if (isRequestSuccessed==false) {
        $("#post-error").html("Please Try Again");
    }
    else
    {
        $("#post-error").html("");
    }
}

The problem is that Javascript's function doesn't firing and isntead the alert, my browser(Firefox) returns me pop-up to download application/json file. What is wrong here?

like image 214
Chuck Norris Avatar asked Sep 02 '11 09:09

Chuck Norris


1 Answers

Make sure you have included the following script to your page:

<script src="@Url.Content("~/Scripts/jquery.unobtrusive-ajax.js")" type="text/javascript"></script>

and that you have enabled unobtrusive javascript in your web.config:

<add key="UnobtrusiveJavaScriptEnabled" value="true" />

This is what makes Ajax.* helpers such as Ajax.BeginForm to work.

like image 57
Darin Dimitrov Avatar answered Sep 18 '22 10:09

Darin Dimitrov