从$阿贾克斯后返回一个PartialView [英] Return a PartialView from $.Ajax Post

查看:82
本文介绍了从$阿贾克斯后返回一个PartialView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下的code;

I have the following code;

        $.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);
            }

在我的控制器;

And in my controller;

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

    }

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

However, when I get back to the page the success alert doesn't happen. Can anyone see the flaw in this logic?

推荐答案

您期望 JSON 阿贾克斯POST ,但在ActionMethod您返回 PartialView

Your expecting JSON in the .Ajax POST, but in the ActionMethod your returning a PartialView?

尝试:

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

这篇关于从$阿贾克斯后返回一个PartialView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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