Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6141109
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T18:12:17+00:00 2026-05-23T18:12:17+00:00

I am just trying to learn how to use JQuery AJAX with ASP.NET MVC

  • 0

I am just trying to learn how to use JQuery AJAX with ASP.NET MVC so I have constructed a page which allows you to add and remove comments to a list. The only thing that doesn’t work is deleting comments that have just been added through an AJAX call. The comments are deleted from the database but the page is reloaded instead of the AJAX being executed. This is only on comments that have just been added through AJAX. The AJAX delete works fine on comments that were already there when the page loaded.

I have a single View, and a Partial View which holds the html for a new comment. This html is then appended to the comments div in the View when a comment is added.

Note: I want this to be javascript independent, so my delete button on each comment is in its own form that will perform a POST if javascript is disabled.

View (Index.aspx)

<%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage<IEnumerable<AJAXComments.Models.Comment>>" %>

<!DOCTYPE html>

<html>
<head runat="server">
    <title>Index</title>
    <link rel="Stylesheet" type="text/css" href="../../Content/Styles/Master.css" />
    <script src="../../Scripts/jquery-1.4.4.min.js" type="text/javascript"></script>
</head>
<body>
    <div id="Container">
        <h2>Comments</h2>
        <div id="Comments">
        <% foreach (var c in Model)
           { %>
            <div class="comment">
                <input class="id" type="hidden" value="<%: c.Id %>" />
                <div class="author">Posted by <%: c.User %></div>
                <div class="comment-text">
                    Posted <%: c.DateTimePosted %>
                    <p><%: c.CommentText %></p>
                    <% using (Html.BeginForm("DeleteComment", "Home", new { id = c.Id }, FormMethod.Post, new { @class = "DeleteForm" }))
                       { %>
                       <input type="submit" value="Remove" />
                    <% } %>
                </div>
            </div>
        <% } %>
        </div>
        <div id="AddComment">
        <% using (Html.BeginForm("AddComment", "Home", FormMethod.Post, new { id = "AddCommentForm" }))
           { %>
            <label for="User">Your Name:</label>
            <input name="User" id="User" type="text" /><br />
            <label for="CommentText">Comment:</label>
            <textarea name="CommentText" id="CommentText" rows="5" cols="10"></textarea><br />
            <input id="Submit" type="submit" value="Submit" />
            <input id="Reset" type="reset" class="hidden" />
        <% } %>
        </div>
        <p class="response"></p>
    </div>
</body>
</html>

Partial View

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<AJAXComments.Models.Comment>" %>

<div class="comment hidden">
    <input class="id" type="hidden" value="<%: Model.Id %>" />
    <div class="author">Posted by <%: Model.User %></div>
    <div class="comment-text">
        Posted <%: Model.DateTimePosted %>
        <p><%: Model.CommentText %></p>
        <% using (Html.BeginForm("DeleteComment", "Home", new { id = Model.Id }, FormMethod.Post, new { @class = "DeleteForm" }))
           { %>
            <input type="submit" value="Remove" />
        <% } %>
    </div>
</div>

Controller ActionResult

[AcceptVerbs(HttpVerbs.Post)]
    public ActionResult DeleteComment(int id)
    {
        var comment = commentsRep.GetCommentById(id);

        if (Request.IsAjaxRequest())
        {
            commentsRep.DeleteComment(comment);

            return Json("Comment removed");
        }
        else
        {
            commentsRep.DeleteComment(comment);

            return RedirectToAction("Index");
        }
    }

JQuery

$(document).ready(function () {

        $('#AddCommentForm').submit(function (e) {
            e.preventDefault();

            var form = {
                'User': $('#User').val(),
                'CommentText': $('#CommentText').val()
            };

            $.ajax({
                url: '/Home/AddComment',
                type: 'POST',
                data: form,
                success: function (html) {
                    $('#Comments').append(html);
                    $('.comment').slideDown('normal', function () {
                        $(this).removeClass('hidden');
                    });
                    $('#Reset').trigger('click');
                }
            });
        });

        $('.DeleteForm').submit(function (e) {
            e.preventDefault();

            var id = $(this).parent().siblings("input.id").val();
            var comment = $(this).parent().parent('.comment');

            $.ajax({
                url: '/Home/DeleteComment',
                type: 'POST',
                data: { id: id },
                success: function (response) {
                    $('p.response').append(response);
                    comment.slideUp('normal', function () {
                        comment.remove();
                    });
                }
            });
        });

    });

Appreciate any help!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T18:12:17+00:00Added an answer on May 23, 2026 at 6:12 pm

    Instead of:

    $('.DeleteForm').submit(function(e) {}); 
    

    You should use:

    $('#Comments').delegate('.DeleteForm', 'submit', function(e) {...});
    

    That will automatically bind the event on new forms loaded to the page.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to learn some C#.net. I'm just trying to expose the AdventureWorks database
I am newbie to jQuery , just trying to learn it since last couple
I am trying to learn some DirectX API and, for now, I have just
I was just trying to learn and understand jQuery source code (so far with
I am new to node.js and I'm just trying to learn how to use
I am somewhat new to jQuery but have been having fun trying learn about
I'm trying to learn some jQuery, and I setup a test page with the
I've just learnt a bit jQuery, and am trying to use it for a
I have just started trying to learn Git and I have got a little
I'm trying to use ELMAH in my console app. I'm just trying to learn

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.