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

Asked

Viewed 281 times

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 post on the page 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 Baixarconta

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

}
  • Your Actionresult has the same name as your controller?

  • 1

    @James S does not have the same name, but I saw in his reply how to proceed, I will implement and then post here the result. Thank you!.

  • @Tiago S sending the data works perfectly, but the ActionResult returns a FileStreamResult that does not work example: Return new Filestreamresult(pdfStream, "application/pdf"); the fact of posting the page in this way influences the non-working of Return ?

  • Yes, totally, but that’s outside the scope of your question. Mark the answer as accepted and if possible an up vote :) and create a new question, for example how to download file via $.ajax()

2 answers

1


Just use @Url.Action()

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

@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>

1

Using @Url.Action where

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

I saw that you created inside a form, you can send the whole form with your data through Submit also, just Action use the Formcollection thus

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

return RedirectToAction("Index") para retorna para a main page
}

Browser other questions tagged

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