How to make a post request in an MVC application to make an integration to a web services?

2
    //
    // GET: /Asientos/Create

    public ActionResult Create()
    {
        return View();
    }

    //
    // POST: /Asientos/Create

    [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult Create(Asientos asientos)
    {
        if (ModelState.IsValid)
        {
            db.Asientos.Add(asientos);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        return View(asientos);
    }
    
asked by Ramon Pereyra 05.08.2016 в 16:05
source

1 answer

1

If I understood correctly you need to invoke using jquery a controller action passing the data by POST, if so, you could implement

Passing JSON objects to Action Methods MVC4?

look at the article as it defines the json that it sends in the data of the $ .ajax so that model binding maps this with the properties of the class that you define per parameter.

The important thing is how to define the $.ajax in client code

var obj = { .. }; //aqui json equivalente a la clase que defines como parametro del action
$.ajax(
{
       url: '@Url.Action("Create","NombreCtrl")',
       type: "POST",
       cache: false,
       dataType: "json",
       contentType: "application/json; charset=utf-8",
       data: JSON.stringify(obj),
       success: function (data) {
           //codigo
      },
      error: function () {
           alert("error");
      }
 });
})

This one explains a little more step by step

Calling ASP MVC Controllers from jQuery Ajax

    
answered by 06.08.2016 в 06:18