Pass TempData

1

Good morning, I have the following problem, I need to pass a variable to another controller and I'm trying to do this for TempData, it follows 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 this:

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

The problem is that every time I execute this method and TempData returns as Null, and I do not know how to solve. If someone can help I thank.

    
asked by anonymous 20.02.2017 / 20:00

1 answer

1

After using the value of the variable 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
    
17.03.2017 / 21:53