Tempdata C# doubt with MVC

Asked

Viewed 1,843 times

5

I know that the TempData has its "life" maintained until it is used in the View. However, if I do it on two different controllers, the same one identified from the TempData, I’m killing and about writing them.

If the user is using 2 browser tabs, it may send the wrong message.

So how can I use the TempData or other functionality, to send data from a Controller to the View, to pass messages, without having the problem with multi tabs. I also did not want to go through URL.

Additional data (Edited)

Code

using (var db = new Conexao())
{
    var usuario = db.Usuario.Find(id);
    var retorno = EntidadeBaseExt.ValidarRegistro(usuario);                
    if (retorno != "")
    {
        TempData["MsgRetornoError"] = retorno;
        return RedirectToAction("Index", "Home");
    }

    return View(usuario);
}

Problem

If the user is using 2 tabs, in the tab 1 it looks for id = 1 and in the tab 2 it looks for id = 3, and both have error return message.

It can happen from the message in tab 2, about writing the message in tab 1, and presenting the wrong information.

This way I would have to pass some way to ensure that tab 1 picks up your message and tab 2 picks up your message.

If I do so:

return RedirectToAction("Index", "Home", new { msg = "Teste " + id.ToString() });

the msg field is in the url and I didn’t want this.

  • Have you tried Viewbag?

  • @Ricardo If you use the command RedirectToAction value is null. This is the problem.

  • Put an example of your code there. Then it’s easier to help.

  • @Ricardo improved the question. But you gave me an idea, to do so: return RedirectToAction("Msg", "Home", new { id = idMensagem });, where id becomes a Session with the data I need. This way the url is cleared, and decreases the cross-data problem.

  • I updated the answer, in the code I put you can pass the ID in the url to search and return the data in Session, will work as you want. If you have solved do not fail to mark the answer.

1 answer

2


Come on, I don’t understand 100% what you want... I’ll try:

I know Tempdata has his "life" held until he is used in View. However, if I do it in two different controllers, the same identified from Tempdata, I’m killing and about writing they.

Every time you enter an Action it executes the code, if it is including the value in TempData["Mensagem"] = "MinhaMensagem"; really it goes on to write.

If the user is using 2 browser tabs, you can send the wrong message.

Each time he runs a URL he executes an action, the same goes for the RedirectToAction, the action that will receive the request will be processed and the case explained above of replacement will occur.

So, how can I use Tempdata or other functionality, to send data from a Controller to the View, to pass messages, without having the multi tabs problem. I also didn’t want to go through URL.

That part wasn’t clear on what would be the problem with multi tabs. I think if you use session will solve your problem, because whatever is placed inside the session in Action 1 will have the value maintained when directed to Action 2 and the view that Action 2 calls may have access to this information.

        public ActionResult Index()
        {

            Session["message"] = DateTime.Now.ToString();
            return RedirectToAction("About","Home");
            //return View();
        }

In this code it will run Action About and the About view can display Session

@Session["message"]

Updating

        public ActionResult Index(string mensagem)
        {

            Session["message"] = mensagem;
            
            return View();
        }

If you use Session it will keep a message per tab... Passes a code to the control and returns in Session the message, it will work.

Browser other questions tagged

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