0

I have created a simple ajax mvc code snippet. The owl idea is to post message from a textarea box to the controller through ajax and update and element on the page with result of the ajax request. But unfortunately the update part of the code is not working.

Problem :- The function (..){} part of the ajax does not execute.

This is the html code

<div id="comments"></div>

     <form method="post" action=@Url.Action("postComment") id="jaidoform">

            <div>Comment: @Html.TextArea("Comment", new {cols=5, rows = 10 })</div>
            <button id="submit">Submit</button>

        </form>

The ajax code

 $('#jaidoform').submit(function (event) {

        event.preventDefault();
        var data = $(this).serialize();
        var url = $(this).attr('action');

        alert(url);
        $.post(url, data, function (response) {

           $('#comments').append(response);

        });

    });

This is the controller handling the ajax process

  public class AjaxController : Controller
    {
        //This container is going to hold a list of comment
        private static List<string> comments = new List<string>();

        //This is going to perform an ajax post from a form to the database
        [HttpPost]
        public ActionResult postComment(string comment){

            comments.Add(comment);

            if(Request.IsAjaxRequest())
            {
                ViewBag.Comment = comment; //This is going to send a comment alone to the view and it would send it as a partial view
                return PartialView();
            }

              return RedirectToAction("index");
        }
    }
3
Contributors
5
Replies
12
Views
4 Years
Discussion Span
Last Post by skliz
0

the function(){} segment is meant to fire on success, it sounds like that may not be happening.

Is there any chance you can debug your script and see what's going on? I normally code in VS2010, which allows for debugging of JavaScript. Determining success or failure status would be my first thought.

Not sure if that helps any. It might not.

0

Dont worry, I have fixed it. It now works. Thanks for your help. I restarted my system and restarted the webserver so it would see new rebuild. Then script started working perfectly well.

0

I recently started using ASP.NET MVC 3 with the new Razor view engine. Everything looks great and cool at first sight. I tried to implement a simple Ajax postback form, without any success. Every time I tried to submit the form, it returned a new blank page with my data on it. A common mistake is to forget referencing the MicrosoftAjax.js and MicrosoftMvcAjax.js libraries in your master layout. After spending half an hour trying to figure out what I might have done wrong, I turned to google and MSDN.

0

yeah tanks I would try. Because I though things were okay. going back to it. I encounter the same problem again.

0

I was able to install the Microsoft Minified Ajax through Nuget Package. but unfortunately I did not see this two javascript ajax library. Please were would I find them. Cause I though they would be in the script package but they not there

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.