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.
?– Maniero
Yes, but then I can’t put Tempdata, it says it doesn’t contain in the current context.
– Brayan
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.
– Maniero
OK vlw, I’ll take a better look here.
– Brayan
How about using Session?
– LP. Gonçalves
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.
– Brayan
It’s an ASP.NET MVC project that you can’t use the library
System.Web.Mvc
? It seems to me well planned.– Jéf Bueno
You should make this clear in the question, to avoid unnecessary answers to your context.
– LP. Gonçalves
Now I started to understand the problem, you’re stuck.
– Maniero
So, if I use global variable would solve the problem?
– Brayan
Personal I managed to solve, I created a global variable Static and solved my problem, even so thank you for everyone’s attention :D
– Brayan
Hello, in this article by Eduardo Píres you can better understand how to use Tempdata.
– Edney Batista da Silva