Spend Tempdata

Asked

Viewed 430 times

1

Good afternoon guys I’m having the following problem, I need to pass a variable to another controller and I’m trying to do this by Tempdata, follow the code:

var user = User.Identity.GetUserId();
var getUser = db.AspNetUsers.FirstOrDefault(x => x.Id.Equals(user));
var userName = getUser.Nome;
TempData["UsuarioTemp"] = UsuarioTemp;

This code above is in the Homecontroller, and I want to call this Tempdata in the Xmlcontroller that would be:

 HomeController ArquivoX = new HomeController();
 var NomeUsuario = ArquivoX.TempData["UsuarioTemp"];

The problem is that every time I run this method and Tempdata comes back as Null, and I don’t know how to fix it. If anyone can help I’d appreciate it.

  • Have you tried without the ArquivoX.?

  • Yes, but then I can’t put Tempdata, it says it doesn’t contain in the current context.

  • There’s something wrong somewhere else, because this is a global object. I need to leave, if no one solves I see better, but I think you’ll need more information from your code.

  • OK vlw, I’ll take a better look here.

  • How about using Session?

  • So it turns out that the code here was made in such a way that I can’t use the System.Web.Mvc library, I would have to do in some way that we don’t use Tempdata, Session, Viewbag, Etc.

  • 2

    It’s an ASP.NET MVC project that you can’t use the library System.Web.Mvc? It seems to me well planned.

  • You should make this clear in the question, to avoid unnecessary answers to your context.

  • Now I started to understand the problem, you’re stuck.

  • So, if I use global variable would solve the problem?

  • Personal I managed to solve, I created a global variable Static and solved my problem, even so thank you for everyone’s attention :D

  • Hello, in this article by Eduardo Píres you can better understand how to use Tempdata.

Show 7 more comments

1 answer

3

After using the variable value TempData["UsuarioTemp"] execute the following method :

TempData.Keep("UsuarioTemp")
TempData.Keep("nomeDaVariavel") //mantem o valor.

TempData.Remove("nomeDaVariavel") // remove o valor. se vc Não executar o   método TempData.Keep("nomeDaVariavel") após utilizar o seu conteúdo ele é removido automaticamente, por isso retorna null

Browser other questions tagged

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