从$ .Ajax Post返回部分视图

时间:2010-08-16 01:30:35

标签: asp.net-mvc jquery

我有以下代码;

        $.ajax({
            url: "/Home/jQueryAddComment",
            type: "POST",
            dataType: "json",
            data: json,
            contentType: 'application/json; charset=utf-8',
            success: function(data){ 
                //var message = data.Message; 
                alert(data);
                $('.CommentSection').html(data);
            }

在我的控制器中;

    [ValidateInput(false)]
    public ActionResult jQueryAddComment(Comment comment)
    {
        CommentSection commentSection = new CommentSection();

        //ya da - ya da 
        // fill the commentsection object with data

        //then
        return PartialView("CommentSection", commentSection);

    }

然而,当我回到页面时,成功警报不会发生。任何人都可以看到这个逻辑中的缺陷吗?

2 个答案:

答案 0 :(得分:26)

您期待JSON中的.Ajax POST,但在ActionMethod中,您返回PartialView

尝试:

$.ajax({
   url: "/Home/jQueryAddComment",
   type: "POST",
   dataType: "html",
   data: json,
   success: function(data){ 
      //var message = data.Message; 
      alert(data);
      $('.CommentSection').html(data);
   }
}

答案 1 :(得分:0)

除非复制错误,否则看起来你错过了一些结束令牌。

       $.ajax({
        url: "/Home/jQueryAddComment",
        type: "POST",
        dataType: "json",
        data: json,
        contentType: 'application/json; charset=utf-8',
        success: function(data){ 
            //var message = data.Message; 
            alert(data);
            $('.CommentSection').html(data);
            } //<-- added close for anonymous function
        }); //<--added close/semicolon for ajax function

此外,您正在进行POST,但您的操作似乎没有[Post]属性。当您在调试器中运行此操作时,您的操作的断点会被命中吗?