Is there any way to match these JQuery values in C # MVC?

0

I have a view in MVC which when clicking on a button I must return to a previous view, obtaining the values of method, controller and form to which I will redirect, I already get the values but I do not know how to assign them in the @Url .Action ().

    // CODIGO DEL CONTROLADOR
    [HttpPost]
    [ActionName("CALP")]
    [DataAccessService("SQL-FYL", "SP_FYL_H_VoBoAprobador")]
    public ActionResult UpdateVoBoCALP(string comentario, int StageID, int ? StageRet)
    {
        Result result = new Result();

        try
        {
            object[] parametros = new object[] { "U", Session["IDHallazgo"], StageID, 1, 1, Convert.ToDateTime(DateTime.Now.ToString("dd/MM/yyyy")), "C.ERIVPE", Session["ID_Sistema"], comentario };
            DataAccessHelper.ExecuteNonQuery("SQL-FYL", "SP_FYL_H_VoBoAprobador", parametros);

            result.Message = "Hallazgo " + Session["IdHallazgo"] + " Actualizado";

            result.isSuccess = true;
        }
        catch (Exception)
        {
            result.isSuccess = false;
        }

        return this.Json(result); //RETORNO COMO JSON
    }
    //CODIGO EN LA VISTA
    $("#BtnRechazar").click(function () {
        $.ajax({
            type: 'post',
            url: '@Url.Action("CALP", "FlujoAprobacion")',
            data: {
                comentario: $("#_txtComentario").val(),
                StageID: $("#_cmbEtapa").val()
            }
        }).done(function (result) {
            var cntrl = result.contr; var met = result.met; var frm = result.frm;
            SolicitudExitosaRechazo(cntrl, met, frm);
        }).fail(function () {
            alert("Error Al Rechazar La Etapa ");
        });
    });

    //FUNCION
    function SolicitudExitosaRechazo(cntrl, met, frm) {
        $.ajax({
            type: "post",
            url:  '@Url.Action(met, cntrl)', //EN ESTA PARTE TENGO EL CONFLICTO @Url.Action ES CODIGO C# Y NO SE COMO ASIGNAR ESTOS VALORES (met, cntrl) QUE ESTAN COMO JQUERY
            data: $('#' + frm).serialize()
        }).done(function (result) {
            $('#mainContent').html(result);
        });
    }
    
asked by Eduardol Rivas 20.02.2018 в 23:50
source

1 answer

0

You must first keep referring to the @ Url.Action as in the first function as in the image, before making the ajax request you declare the variable params that will have all the variables that You will send and send serialized (this in case you only send parameters and / or models).

//Vista
function SolicitudExitosaRechazo(cntrl, met, frm) {
    // declaras una variable params que tendra todas los parametros que enviaras
    var params = {
        cntrl:cntrl,
        met: met, 
        frm: frm
    };
    $.ajax({
        type: "post",
        contentType: "application/json", // le dices que enviaras el contenido de la peticion en json
        url: '@Url.Action("MyAction", "MyController")', 
        data: JSON.stringify(params), //serializas la variable params
    }).done(function (result) {
        $('#mainContent').html(result);
    });
}

If you want to send a Form (frm.serialize ()) and also other parameters then you will have to do it in the following way:

// en el la VISTA
function SolicitudExitosaRechazo(cntrl, met, frm) {

    $.ajax({
        type: "post",
        url: '@Url.Action("MyAction", "MyController")', 
        data: $('#' + frm).serialize() + "&cntrl=" + cntrl+ "&met=" + met ,
    }).done(function (result) {
        $('#mainContent').html(result);
    });
}


//En tu controller
       [HttpPost]
    public ActionResult MyAction( FormCollection frm,  string cntrl, string met)
    {
        // mi actionresult
    }
    
answered by 22.02.2018 / 01:20
source