Receive post from one view to another view

2

I have two Views.

  • Index
  • Assincrona
  • When accessing index , it calls View Assincrona and gets a gif loading until the whole page ( Assincrona ) is loaded and shows 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 fine until I need to receive data via post.

    I get the value of the month via [HttpPost]Index , but I can not get to Assincrona when it is called via View .

    Would using TempData[] be a good way to solve this problem or is there another way to do it?

        
    asked by anonymous 12.05.2016 / 18:12

    1 answer

    0
      

    Would using TempData[] be a good way to solve this problem or is there another way to do it?

    No. The problem is another here. You are doing load of something you do not have and can not receive arguments. In fact, load is not what you want.

    Correct would be or pass these arguments by GET or 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("");
        });
    });
    

    When you do this, type an Action that accepts [HttpPost] :

    [HttpPost]
    public ActionResult Assincrona(ParametrosViewModel viewModel) { ... }
    
        
    15.07.2016 / 16:05