You should avoid TempData in this case. If you know you are going to need the value in more than 1 controller action then TempData is not for you because it will be erased once you access it (disclaimer: if you use Peek() it will be persisted but that's no the discussion).
What I think can work for you is to redirect with a parameter in your URL according to the result of the operation. You can do something like this:
public async Task<ActionResult> Login(LoginViewModel model, string returnUrl)
{
var result = await SignInManager.PasswordSignInAsync(model.Email, model.Password, model.RememberMe, shouldLockout: false);
switch (result)
{
case SignInStatus.Success:
TempData["data"] = "Login Success";
return RedirectToAction("Action", new { loginSuccessful = true });
default:
ModelState.AddModelError("", "Invalid login attempt.");
return View(model);
}
}
Then in controller2 you would have your code like this:
public class SampleController : Controller
{
public SampleController()
{
}
public ActionResult Index(bool loginSuccessful)
{
if (loginSuccessful)
{
ViewBag["message"] = "Login successful";
}
return View();
}
}
If any other controller action needs that parameter you just add it to the function signature as I did in the Index
action and it will work as long as the parameter is still in the URL.