在ASP.NET MVC中通过jQuery提交表单时传递参数

我正在尝试通过jQuery Ajax向我的控制器提交表单。 以下代码大部分都有效,但是,ThreadId参数不会被传递。 如果我直接调用控制器而不使用jQuery,它会被传递,但是当使用jquery时,我看不到form.serialize()之后的ThreadId。 什么是将参数(如ThreadId)传递给jQuery表单post的最简单方法?

ASPX

    

JavaScript的

  AddComment = function (sender) { var form = $(sender); var data = form.serialize(); $.ajax({ type: "POST", url: "/Home/AddComment", data: data, dataType: "html", success: function (response) { alert(response); }, error: function (error) { alert(error); } }); return false; }; 

CONTROLLER

  [HttpPost] public ActionResult AddComment(string submitButton, Comment comment) { comment.CreatedDate = DateTime.Now; comment.PosterId = LoggedInUser.Id; _repository.AddComment(comment); _repository.Save(); if (Request.IsAjaxRequest()) { return View("Comment", comment); } else return RedirectToAction("Index"); } 

ThreadId参数包含在表单的action属性中。 当您正在调整此表单时,您将发布到/Home/AddComment并且不再提供此参数。 您可以执行以下操作来解析它:

 $('#idofyourform').submit(function() { $.ajax({ // use the method as defined in the 

另一种可能性是将表单内的ThreadId参数包含在隐藏字段中,而不是将其放在action属性中。