Send data to an ActionResult from a Controller other than the current one

1

How to make a post sending data to a ActionResult of a Controller different from the current%? Example:

I have a link called lnkEnviarDados and I need to make a post on the page by sending the contents of the variables when I click on this link.

@using (Html.BeginForm("Index", "Pessoa", FormMethod.Post))
{
    <table class="grid">
        <tbody>
            <tr>
                <td>
                    <a href="#" class="lnkEnviarDados" name="downloaditem" id="downloaditem2" data-id_Atributo_1="6" data-id_Atributo_2="1" data-id_Atributo_3="2" target="_blank">
                        <span class="idSpan">Regular</span>
                    </a>
                </td>
            </tr>
        </tody>
    </table>
}

@section Scripts{
<script type="text/javascript">
    $(document).ready(function () {         
        $(document).on('click', '.lnkEnviarDados', function (e) {
            e.preventDefault();

            var _Atributo_1 = $(this).attr("data-Atributo_1");
            var _Atributo_2 = $(this).attr("data-Atributo_2");
            var _Atributo_3 = $(this).attr("data-Atributo_3");

            //Fazer um post deste controller para enviar os dados acima
            //para um outro Controller chamado "BaixarConta"

        });
    }
}

CONTROLLER DownloadContact

[HttpPost]
public ActionResult BaixarConta(int _Atributo_3, int _Atributo_2, int _Atributo_3)
{

}
    
asked by anonymous 11.06.2017 / 15:17

2 answers

1

Just use @ Url.Action ()

The first parameter is the action and the second the controler , that is,

  

@ Url.Action ("ACTION NAME", "CONTROLLER NAME")

Using $ .ajax () would look like this:

<script type="text/javascript">
    $(document).ready(function () {         
        $(document).on('click', '.lnkEnviarDados', function (e) {
            e.preventDefault();

            var _Atributo_1 = $(this).attr("data-Atributo_1");
            var _Atributo_2 = $(this).attr("data-Atributo_2");
            var _Atributo_3 = $(this).attr("data-Atributo_3");

            $.ajax({
                url:'@Url.Action("BaixarConta","BaixarConta"),
                method:'post',
                data:{_Atributo_1:_Atributo_1 ,_Atributo_2:_Atributo_2,_Atributo_3:_Atributo_3}
                success:function(retorno){
                      //TODO:Implemente as suas funções para atualizar a tela.
                });


        });
    }
</script>
    
11.06.2017 / 15:32
1

Using the @ Url.Action where

 @Url.Action("NomeDaView", "NomeDoController", new {Attributo1 = valor, atributo2 = valor2, atributo3 = valor3}

I have seen that you created inside a form, you can send the entire form with your data through the submit as well, just use Action to use the FormCollection and thus

[HttpPost]
public ActionResult BaixarConta(FormCollection Form)
{
var teste = Form["Attr1"];

return RedirectToAction("Index") para retorna para a main page
}
    
11.06.2017 / 17:35