大佬教程收集整理的这篇文章主要介绍了jquery – Ajax将null Json对象发布到mvc 4控制器,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
干杯
这是我的模特
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; }
强类型视图
@model CommentModel @using (Html.beginForm("Index","Home",FormMethod.Post,new { id = "form1" })) { @Html.TextBoxFor(x => x.EmailAddress) @Html.TextBoxFor(x => x.Name) ... }
脚本
$(function () { $('form').submit(function () { if ($(this).valid()) { $.ajax({ url: this.action,type: this.method,data: $(this).serialize(),beforeSend: function () { },complete: function () { },success: function (result) { },error: function () { } }); } return false; }); });
调节器
[httpPost] public ActionResult Index(CommentModel commentModel) { int i = commentModel.ActivityId; String k = commentModel.CommentText; return View(); }
以上是大佬教程为你收集整理的jquery – Ajax将null Json对象发布到mvc 4控制器全部内容,希望文章能够帮你解决jquery – Ajax将null Json对象发布到mvc 4控制器所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。