Receive post from one view to another view

Asked

Viewed 306 times

3

I have two Views.

  1. Index
  2. Assincrona

When access to index, he calls the View Assincrona and there’s a gif uploading to the whole page (Assincrona) be loaded and displayed in View.

Controller Codes

public ActionResult Assincrona()
    {
        DateTime data = DateTime.Now;
        mes = Convert.ToInt32(data.Month) - 2;
        dadosCarteiraPGC(2016, mes, null);

        return PartialView("Assincrona", carteiraPGC);
    }

public ActionResult Index()
    {
        return View();
    }

View codes

<div class="partialContents" data-url="/Consulta/Assincrona">
            <div class="mensagem"><img src="~/Images/indicator.white.gif"/><p>Carregando ... </p></div>
</div>

JS codes

var site = site || {};
site.baseUrl = site.baseUrl || "";

$(document).ready(function (e) {    
$(".partialContents").each(function(index, item) {
    var url = site.baseUrl + $(item).data("url");
    if (url && url.length > 0 ) {
        $(item).load(url);
    }
});

$("a.nav").click(function() {
    $("body").html("");
});
});

It’s working perfect until I need to receive data via post.

I recover the value of the month via [HttpPost]Index, but I can’t get through to Assincrona when she is called via View.

Use TempData[] would be a good way to solve that problem or is there another way to do?

1 answer

1

Use TempData[] would be a good way to solve that problem or is there another way to do?

No. The problem is another one here. You’re making one load of something that does not have and cannot receive arguments. In fact, load that’s not what you want.

The correct would be to either pass these arguments by GET or by POST. Possibly you want something more restricted, so the recommended is POST. The call therefore needs to be using $.ajax():

var site = site || {};
site.baseUrl = site.baseUrl || "";

$(document).ready(function (e) {    
    $(".partialContents").each(function(index, item) {
        var url = site.baseUrl + $(item).data("url");
        if (url && url.length > 0 ) {
            $.ajax({
                method: "POST",
                url: url,
                data: { campo1: "Valor1", campo2: 1 } // Coloque aqui seus campos
            })
            .done(function( msg ) {
                alert( "Data Saved: " + msg );
            });
        }
    });

    $("a.nav").click(function() {
        $("body").html("");
    });
});

Done that, write a Action that accepts [HttpPost]:

[HttpPost]
public ActionResult Assincrona(ParametrosViewModel viewModel) { ... }

Browser other questions tagged

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