从$ .Ajax Post返回PartialView

gri*_*egs 17 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);
            }
Run Code Online (Sandbox Code Playgroud)

在我的控制器;

    [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);

    }
Run Code Online (Sandbox Code Playgroud)

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

xan*_*ded 28

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);
   }
}
Run Code Online (Sandbox Code Playgroud)