Does Tempdata lose property when using Response.Redirect?

Asked

Viewed 180 times

2

I have the following code on the pages that, to be accessed, it is necessary to log in:

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 on the login screen:

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

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

  • For reference: https://answall.com/questions/149665/passagem-de-par%C3%A2metros-com-Asp-net-mvc/149674#149674

1 answer

3


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

On the controller:

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

In view:

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

Browser other questions tagged

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