Call Action via javascript

Asked

Viewed 2,004 times

0

Good afternoon, I need help with a problem until simple, but it’s knocking me down.

I need to call a controller method and pass two parameters to it, I did something, but it’s not working, someone knows where I’m going wrong?

View

<input type="submit" value="Confirmar" name="DuploSIM" id="btnDS" class="btn btn-info btn-lg" />

Script

$('#btnDS').click(function () {
    var pass = $('#txtDuplo').val()
    var action = $('#btnClick').val()
    if (pass == null) {
        alert('O campo de senha deve ser preenchido.');
        return false;
    }
    if (pass.length != 8) {
        alert('O campo de senha deve conter 8 caracteres.');
        return false;
    }

    var resultado = { "pass": pass, "index": action };

    $.ajax({
        url: '@Url.Action("DuploSIM", "Renegociacao")',
        datatype: 'json',
        contentType: "application/json; charset=utf-8",
        type: "POST",
        data: JSON.stringify(resultado), 
        success: function (data) {
            alert('sucesso!!');
        },
        error: function (error) {
            loading(0, "");
        }
    });
    return false;
});

Controller

[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult DuploSIM(string pass, string index)
{
    FormularioModel model = (FormularioModel)Session["modelReg"];

    try
    {

        if (string.IsNullOrEmpty(pass))
            return RedirectToAction("Index", "Renegociacao", new { idOperacao = model.idOperacao, msg = "É necessário preencher o campo senha antes de confirmar." });
        else
            if (ValidarSenhaSuperior(Convert.ToInt32(pass), Convert.ToInt32(model.funcional)))
            {
                return RedirectToAction(index, "Renegociacao", new { model = model });                        
            }
            else
                return RedirectToAction("Index", "Renegociacao", new { idOperacao = model.idOperacao, msg = "Senha não confere com o superior." });
    }
    catch (Exception ex)
    {
        throw new Exception(ex.Message);
    }
}
  • In your url you have to call your Controller/Action and not the Action, example: url: '/api/Controller/DuploSIM'. But here’s how to configure the path to the API.. This is in the "App_start folder".

  • Your problem has been solved ?

  • was yes... thank you all.

1 answer

1

Try this:

$('#btnDS').click(function () {
var pass = $('#txtDuplo').val()
var action = $('#btnClick').val()
if (pass == null) {
    alert('O campo de senha deve ser preenchido.');
    return false;
}
if (pass.length != 8) {
    alert('O campo de senha deve conter 8 caracteres.');
    return false;
}

var resultado = { "pass": pass, "index": action };

$.ajax({
    url: '/DuploSIM/Renegociacao',
    datatype: 'json',
    contentType: "application/json; charset=utf-8",
    type: "POST",
    data: JSON.stringify(resultado), 
    success: function (data) {
        alert('sucesso!!');
    },
    error: function (error) {
        loading(0, "");
    }
});
return false;
});
  • Thanks for the help, the problem was solved by a guy from my team, he modified the method by changing to the return Json.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.