TempData loses property when using Response.Redirect?

2

I have the following code in the pages that, to be accessed, you need to login:

if (Session["Login"] == null)
{
    TempData["msg"] = "É necessário realizar o login para acessar esta página!";
    Response.Redirect("/Login/Index");
}

This TempData["msg"] is as follows in the login screen:

@if (TempData["msg"] != null)
{
    <div class="alert alert-danger">
       @TempData["msg"]
    </div>
}

However, the message is not appearing, as if TempData was canceling. What do I have to do to get the message to the login screen?

    
asked by anonymous 11.04.2017 / 13:05

1 answer

3

Use ViewBag because it exists until the end of response execution.

No controller:

if (Session["Login"] == null)
{
    ViewBag.msg = "É necessário realizar o login para acessar esta página!";
    Response.Redirect("/Login/Index");
}

Na view:

@if (TempData["msg"] != null)
{
    <div class="alert alert-danger">
       @ViewBag.msg
    </div>
}
    
11.04.2017 / 13:20