Jquery ajax 发布请求将 null json 对象发布到 mvc 控制器。知道为什么会这样吗?
干杯
这是我的模型
public class CommentModel
{
public string EmailAddress { get; set; }
public string Name { get; set; }
public int ActivityId { get; set; }
public string CommentText { get; set; }
}
控制器
[HttpPost]
public ActionResult Index(CommentModel commentModel)
{
int i = commentModel.ActivityId;
string k = commentModel.CommentText;
return View();
}
jQuery
$("#CommentForm").submit(function () {
var formDataAsJson = GetFormDataAsJson();
$.ajax({
url: $(this).attr("action"),
dataType: 'json',
type: "POST",
data: JSON.stringify({ commentModel: formDataAsJson }),
contentType: 'application/json; charset=utf-8',
success: function (data) {
$("#commentsection").append(data);
}
})
});
function GetFormDataAsJson() {
var emailInput = $("#InputEmailAddress").attr("value");
var name = $("#InputName").attr("value");
var comment = $("#some-textarea").attr("value");
var activityid = parseInt($("#ActivityID").attr("value"));
var formObject = {
EmailAddress: emailInput,
Name: name,
ActivityId: activityid,
CommentText:comment
}
return formObject;
}