How can I call controller post actions from jquery (on user view page) in mvc.net web application

I am creating an event management web application using .net mvc and jquery. I created a web application mvc web.contoller with the name SeatPlansControllerand model with the name SeatPlanes, and I can insert the data into the database. But I can not transfer data from jquery to the database using the mvc controller.

My controller action code is shown below

    // 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);
        }


   

In post create controller Id is the primary key, so I want to convey seat_id, seat_noas an argument and it should update the database.

I used the following 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;
        }
    });
}

I can run the create get method with

http://localhost:52348/SeatPlans/Create

but how can I run the post method directly from the browser with an argument something like

http://localhost:52348/SeatPlans/Create/2/3t/e

script, , GET, TYPE: "post", "localhost: 52348 :   "




     $(document).ready(function () {
            $("button").click(function () {
                $.ajax({
                    type: "POST",
                    contentType: "application/json; charset=utf-8",
                    url: '@Url.Action("Create", "SeatPlans", new { Area = "" })',
                    data: { seat_id: "34", seat_no: "98" },
                    dataType: "json",
                    async: false,
                    success: function (result) {
                        $("#div1").html(result);
                    },

                    error: function (abc) {
                    alert(abc.statusText);
                    },



                });
            });
        });




+4
1

, script

        //jQuery.noConflict();
      //  var $j = jQuery.noConflict();

        function clickevent()
        {
            var form = $("#frm");
            var token = $('input[name="__RequestVerificationToken"]', form).val();
                $.ajax({
                    type: "post",
                  //  headers: { "__RequestVerificationToken": token },
                    url: '@Url.Action("Create", "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);
    }

+1

All Articles