Usage Tempdata ASP MVC

Asked

Viewed 630 times

0

What I’ve been researching (here), I can save a data list in a Tempdata created in one function, to use in another function in my Controller.

All right, I’m trying to do just that, I’m just not getting it:

Model:

...
namespace ARTSOFT.dal.ViewModels
{
    public class Localidades
    {
        public string Familia { get; set; }
        public string Subfamilia { get; set; }
        public string Registo { get; set; }
        public string Descricao { get; set; }
    }

    public class LocalidadesFormViewModel
    {
        public Localidades Localidades { get; private set; }

        public LocalidadesFormViewModel(Localidades localidades)
        {
            Localidades = localidades;
        }
    }
}

Function where I create Tempdata:

public ActionResult AtribuirOrdemServ()
{
    ...
    var newListaLocalidades = new List<ARTSOFT.dal.ViewModels.Localidades>();
    foreach (var item in localidades)
    {
       var newItemLocalidade = new ARTSOFT.dal.ViewModels.Localidades();
       newItemLocalidade.Familia = item.ToString().Split(',')[0].Split('{')[1].Split('=')[1].Split(' ')[1];
       newItemLocalidade.Subfamilia = item.ToString().Split(',')[1].Split('=')[1].Split(' ')[1];
       newItemLocalidade.Registo = item.ToString().Split(',')[2].Split('=')[1].Split(' ')[1];
       newItemLocalidade.Descricao = item.ToString().Split(',')[3].Split('}')[0].Split('=')[1].Split('"')[0];
       newListaLocalidades.Add(newItemLocalidade);
   }

   TempData["ListaLocalidades"] = newListaLocalidades.ToList();

   return View("IndexAtribuirOrdemServ");
}

Then I’m trying to access another function, via Javascript, and wanted to search this Tempdata:

$("#divResultFiltroAtribuirOrdemServ").load("FiltroAtribuirOrdemServ"
, function () {
...
});

And the function FiltroAtribuirOrdemServ:

public ActionResult FiltroAtribuirOrdemServ(){
    ...
    var teste = (Localidades)TempData["ListaLocalidades"];
}

Where do I get the TempData["ListaLocalidades"] at all times null

1 answer

0


You are casting a Localities object, but you should actually be casting a Localities List:

(List<Localidades>)TempData["ListaLocalidades"];

Instead of using the method View, use RedirectToAction, so he’ll go through ActionResult that you created and want to work with the data.

return RedirectToAction("IndexAtribuirOrdemServ");

The way you did it goes straight to View without entering Actionresult, you can also work with the data in View:

@foreach (var item in (List<ARTSOFT.dal.ViewModels.Localidades>)TempData["ListaLocalidades"])
{
    @Html.Raw(item.Descricao)
}
  • You’re right and it was stupid of me that I didn’t notice, the situation is that the TempData["ListaLocalidades"] is null, so it doesn’t solve my problem

  • I tested here worked normally, you want to pass this Tempdata pro Controller Indexatribuirordemserv?

  • No, I want to pass AtribuirOrdemServ() to View(), and then to FiltroAtribuirOrdemServ() access data. This last function calls from Javascript

  • I edited the answer, see if it fits what you want.

  • The point is, one function has nothing to do with another. One loads a page (Attributrordemserv()), and the other (Fileattributrordemserv()) loads a partial with results of filters applied by the user. When doing Redirecttoaction lose the sense of functionality

  • If I say that I just tested created the views, I added its function and in Actionresult the List<Localities> came populated.

  • ended up loading the list again in the function FiltroAtribuirOrdemServ() and I tried to optimise as much as possible some queries that I had so there would be no waste of time. I will validate your answer because it seems to be the right one

Show 3 more comments

Browser other questions tagged

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