加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

asp.net – 如何从mvc .net web app中的jquery(在自定义视图页面

发布时间:2020-12-16 09:42:12 所属栏目:asp.Net 来源:网络整理
导读:我正在使用.net mvc和 jquery创建一个用于事件管理的Web应用程序. 我创建了一个名为SeatPlansController的mvc web app.contoller和名为SeatPlanes的模型,我可以将数据插入数据库. 但是我无法使用mvc控制器将数据从jquery传递到数据库. 我的控制器动作代码如
我正在使用.net mvc和 jquery创建一个用于事件管理的Web应用程序.
我创建了一个名为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);
    }

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读