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
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– CesarMiguel
I tested here worked normally, you want to pass this Tempdata pro Controller Indexatribuirordemserv?
– Laerte
No, I want to pass
AtribuirOrdemServ()
to View(), and then toFiltroAtribuirOrdemServ()
access data. This last function calls from Javascript– CesarMiguel
I edited the answer, see if it fits what you want.
– Laerte
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
– CesarMiguel
If I say that I just tested created the views, I added its function and in Actionresult the List<Localities> came populated.
– Laerte
Let’s go continue this discussion in chat.
– CesarMiguel
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– CesarMiguel