ASP.NET MVC AJAX使用jQuery [英] ASP.NET MVC AJAX with jQuery

查看:110
本文介绍了ASP.NET MVC AJAX使用jQuery的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个网站,每个用户的页面显示意见和允许其他用户添加注释。我想拥有它,因此添加注释的形式是在页面上,当用户添加一个评论,它被添加到数据库中并与AJAX注释部分显示出来。我使用jQuery的AJAX和LINQ to SQL来处理数据库逻辑。怎么会去这样做,这样的注释被添加到数据库后,评论部分被刷新,更新,而无需刷新页面?

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?

推荐答案

您需要采取的'成功'的优势(或完整)由jQuery的Ajax调用燃煤火后续的AJAX调用事件刷新你的评论的内容。这可能会是这个样子(翅吧,未经测试):

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();                    
            }
        }
    });

修改的JSON code将利用jQuery插件的jQuery-JSON(中的 HTTP://$c$c.google.com/p/jquery-json/

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

这篇关于ASP.NET MVC AJAX使用jQuery的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆