我正在使用.net mvc和
jquery创建一个用于事件管理的Web应用程序.
我创建了一个名为SeatPlansController的mvc web app.contoller和名为SeatPlanes的模型,我可以将数据插入数据库.
但是我无法使用mvc控制器将数据从jquery传递到数据库.
我创建了一个名为SeatPlansController的mvc web app.contoller和名为SeatPlanes的模型,我可以将数据插入数据库.
但是我无法使用mvc控制器将数据从jquery传递到数据库.
我的控制器动作代码如下所示
// GET: SeatPlans/Create public ActionResult Create() { return View(); } // POST: SeatPlans/Create // To protect from overposting attacks,please enable the specific properties you want to bind to,for // more details see http://go.microsoft.com/fwlink/?LinkId=317598. [HttpPost] [ValidateAntiForgeryToken] public ActionResult Create(String seat_id,String seat_no) { int id = 10; SeatPlans S = new SeatPlans(); S.seat_id = seat_id; S.seat_no = seat_no; if (ModelState.IsValid) { db.SEATPLAN.Add(S); db.SaveChanges(); // return RedirectToAction("Index"); } return View(S); }
在帖子创建控制器Id是主键,所以我想传递seat_id,seat_no作为参数,它应该更新数据库.
我使用了以下javascript
function getPerson(id) { $.ajax({ type: "GET",url: '@Url.Action("create","SeatPlanesController")',contentType: "application/json; charset=utf-8",data: {eat_id :6,seat_no:8},dataType: "json",success: function (result) { alert(result); //window.locationre = result.url; } }); }
我可以使用运行create get方法
http://localhost:52348/SeatPlans/Create
但是如何直接从带有参数的浏览器运行post方法
就像是
http://localhost:52348/SeatPlans/Create/2/3t/e
我已将脚本更改为下方,它适用于GET方法,但如果我使用TYPE:“post”,则会弹出一个带警报的警告框
“localhost:52348说:
内部服务器错误”
$(document).ready(function () { $("button").click(function () { $.ajax({ type: "POST",url: '@Url.Action("Create","SeatPlans",new { Area = "" })',data: { seat_id: "34",seat_no: "98" },async: false,success: function (result) { $("#div1").html(result); },error: function (abc) { alert(abc.statusText); },}); }); });
解决方法
最后我得到了溶剂
我改变了脚本如下
我改变了脚本如下
//jQuery.noConflict(); // var $j = jQuery.noConflict(); function clickevent() { var form = $("#frm"); var token = $('input[name="__RequestVerificationToken"]',form).val(); $.ajax({ type: "post",// headers: { "__RequestVerificationToken": token },"SeatPlans")',data: { seat_id: "34",seat_no: "98" },success: function (result) { $("#div1").html(result); } }); }
然后将创建帖子方法更改为以下方式
<pre> public ActionResult Create(String seat_id,String seat_no) { int id = 10; SeatPlans S = new SeatPlans(); S.seat_id = seat_id; S.seat_no = seat_no; if (ModelState.IsValid) { db.SEATPLAN.Add(S); db.SaveChanges(); // return RedirectToAction("Index"); } return View(S); }
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。