Call an ActionResult from Jquery

0

From Jquery I want to call an ActionResult from a controller, but it does not arrive. That ActionResult does not belong to any view, I just want to change a data in BBDD. How could I get it?

The error that the javascript console leaves:

  

Failed to load resource: the server responded to a status of 500   (Internal Server Error)

Here I leave the code.

$(".abrirCorreo").click(function () {
        var id = $(this).attr("id");
        var miUrl = '@Url.Action("Confirmacion", "Correo")'
        $.ajax({
            url: miUrl,
            method: "post",
            data: id,
            success: function (response) {

            },
            async: true
        });


    })


 [HttpPost]
    public ActionResult Confirmacion(int idCorreo)
    {

        DBEntities db = new DBEntities();

            Correo correo = db.Correo.Where(a => a.IdCorreo == idCorreo).First();

            if (!correo.Leido)
            {
                correo.Leido = true;
                db.SaveChanges();
            }
            return View();


    }
    
asked by Borja Calvo 08.09.2016 в 12:11
source

1 answer

1

Let's start from the basis that an ActionResult is not invoked, but is the answer. What you are invoking is a action of a controller .

The definition of ajax seems correct but you do not send the data in an appropriate way, what happens if you use

var id = $(this).attr("id");

var params = { idCorreo: id };                  

$.ajax({
    url: miUrl,
    method: "post",
    data: params,
    success: function (response) {

    },
    async: true
});

You have to arm the json so that the value matches the action parameter, so idCorreo

If it does not work that way it's used in ajax

data: JSON.stringify(params),

JSON.stringify ()

    
answered by 08.09.2016 / 13:21
source