How to call controller action?

Asked

Viewed 2,499 times

1

I have the following code:

function confirmaExclusao() {
    var decisao = confirm("Deseja excluir permanentemente esse item?");
    if(decisao == true)
    {
        //Chamar a action para excluir
    }
    else
    {
        //Aqui não vai fazer nada!
    }
}

I need to call a ActionResult of a Controller in C# sending an argument, how can I do that?

Like a RedirectToAction.

  • With the RedirectToAction doesn’t work? something like return RedirectToAction("TuaAcao", new { argumento = valor });.

  • I haven’t been able to use Redirecttoaction in View, not even appears in auto complete.

  • RedirectToAction is a class method ApiController - therefore, it is not available in the view. Also, even if it were available, it would be useless. The method returns an instance of RedirectToRouteResult, that has no meaning in the context of Javascript.

1 answer

1


You can call the action as follows.

$.ajax
({ 
    url: '/controller/action',
    type: 'GET', (ou method que precisa)
    success: function (dados) {
        var resultado = dados; // Caso vá retornar alguma coisa
    },
    error: function (erro) {

    }
});
  • I would like an alternative in Razor...

  • @Jedaiasrodrigues There is no alternative, you need to make a request for Ajax. The only thing you can improve on the code is 'use @Url.RouteUrl("RouteName") (if you use attribute-based routing) or @Url.Action("ActionName") instead of typing the URL manually '/controller/action'.

Browser other questions tagged

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