Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET MVC AJAX with jQuery

Tags:

I have a site where each user's page shows comments and allows other user's to add comments. I want to have it so the add comments form is on the page and when a user adds a comment, it is added to the database and shows up in the comment section with AJAX. I am using jQuery for the AJAX and LINQ to SQL to handle the database logic. How would go about doing this so that after the comment is added to the database, the comments section is refreshed and updated without refreshing the page?

like image 654
roflwaffle Avatar asked Feb 23 '09 17:02

roflwaffle


People also ask

Can we implement Ajax using jQuery in MVC?

Using AJAX In ASP.NET MVC. Implementation of Ajax can be done in two way in ASP.Net Application: using Update Panel and, using jQuery.

Can we use Ajax in MVC?

The MVC Framework contains built-in support for unobtrusive Ajax. You can use the helper methods to define your Ajax features without adding a code throughout all the views. This feature in MVC is based on the jQuery features. To enable the unobtrusive AJAX support in the MVC application, open the Web.

How can we call MVC action using jQuery Ajax and then submit form in MVC?

don't use <input type="submit"..> . try <button id="btnSave">Save</button> your javascript will trigger on btnSave click. Then in your ajax success callback, evaluate if data. status == "Success" then grab your form field values and build a new json object with it and do a new AJAX call.


2 Answers

You would need to take advantage of the 'success' (or 'complete') event that is fired by the jQuery ajax call to fire a subsequent AJAX call for refreshing the content of your reviews. This would probably look something like (winged it, untested):

function UpdateComments(){
    resultHTML = jQuery.ajax({
        type: 'GET',
        url: 'Comments/List/UserID'
    }).responseText;

    $('#comments').html(resultHTML);
}

function PostComment(targetUserID, commenterUserID, comment)
jQuery.ajax({
        type: 'POST',
        contentType: 'application/json; charset=utf-8',
        data: $.toJSON({review: comment, id:targetUserID, commenter:commenterUserID}),
        dataType: 'json',
        url: 'Comments/Add',
        success: function(result){
            // Only update comments if the post was successful:
            resultJson = $.evalJSON(result);
            if(resultJson['success'] == true){
                UpdateComments();                    
            }
        }
    });

EDIT The JSON code would make use of the jquery plugin jquery-json (http://code.google.com/p/jquery-json/)

like image 103
Matt Avatar answered Oct 12 '22 01:10

Matt


In response to Matt, another way to submit the form data is, instead of the JSON, you could call $('#form').serialize() in the 'data' field of the jQuery.ajax function. This would eliminate the need for a plugin.

Also, I'm not an expert on this subject, still trying to learn it myself, but is it necessary to have both a POST and GET request when you could insert the response from ASP.NET MVC into the page instead? This would result in one request. There might be a valid reason for that approach though. I guess mine would look like this:

    // The Controller Action should return a PartialView as response,
    // so just a user control that contains the comments. 
function PostComment(targetUserID, commenterUserID, comment)
jQuery.ajax({
    type: 'POST',
    data: $('#commentForm').serialize(),
    url: 'Comments/Add',
    success: function(result){ 
        $('#comments').html(result);


        }
    }
    });
like image 39
JulianR Avatar answered Oct 12 '22 01:10

JulianR