尝试使用asp.net核心MVC通过AJAX添加记录;通过jQuery传递时,模型为空

尝试使用asp.net核心MVC通过AJAX添加记录;通过jQuery传递时,模型为空,jquery,asp.net-core-mvc,Jquery,Asp.net Core Mvc,我肯定我忽略了一些显而易见的事情,但问题是。我有一个添加注释功能,我想异步处理它,为了简单起见,它需要两个属性:PostId和CommentText 我在控制器中设置了以下处理程序: [HttpPost] public IActionResult AddComment(AddCommentModel acm) { //fun stuff goes here return PartialView("CommentList",scl

我肯定我忽略了一些显而易见的事情,但问题是。我有一个添加注释功能,我想异步处理它,为了简单起见,它需要两个属性:PostId和CommentText

我在控制器中设置了以下处理程序:

[HttpPost]
public IActionResult AddComment(AddCommentModel acm) {

                //fun stuff goes here

                return PartialView("CommentList",scl);
}
我的jQuery调用如下:

$btnPostComment.click 作用{

                var comment = $("#userComment").val();
                var id = $("#postId").val();



                $.ajax({
                    url: "/Post/AddComment",
                    type: "post",
                    data: JSON.stringify({ acm: { Comment: comment, PostId: id } }),
                    contentType: "application/json; charset=utf-8",
                    success: function (result) {
                        $("#partial").html(result);
                    },
                    error: function (xhRequest, ErrorText, thrownError) {
                        alert("Failed to process promotion correctly, please try again");
                        console.log('xhRequest: ' + xhRequest + "\n");
                        console.log('ErrorText: ' + ErrorText + "\n");
                        console.log('thrownError: ' + thrownError + "\n");
                    }
                });
            }
        );

我遗漏了什么明显且令人尴尬的东西?

请用此代码替换您的代码,然后查看

在控制器中

[HttpPost]
public IActionResult AddComment(string Comment, int PostId) {

    //fun stuff goes here

    return PartialView("CommentList",scl);
}
点击按钮

$.ajax({
    url: "/Post/AddComment",
    type: "post",
    data: { Comment: comment, PostId: id },
    success: function (result) {
        $("#partial").html(result);
        },
    error: function (xhRequest, ErrorText, thrownError) {
        alert("Failed to process promotion correctly, please try again");
        console.log('xhRequest: ' + xhRequest + "\n");
        console.log('ErrorText: ' + ErrorText + "\n");
        console.log('thrownError: ' + thrownError + "\n");
        }
      });

这就是我的工作原理:

[HttpPost]
        public IActionResult AddComment([FromBody]AddCommentModel acm)

谢谢,我在发帖前就试过了,结果也一样。