TempData Doubt C # with MVC

4

I know that TempData has its "life" held until it is used in View . However, if I do in two different controllers, the same identified TempData , I'm killing and over writing them.

If the user is using 2 browser tabs, he may issue the wrong message.

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

Supplementary 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, on tab 1 he looks for id = 1 and on tab 2 he searches for id = 3, and both have an error message.

It may happen that the message in tab 2, writing the message in tab 1, and presenting the wrong information.

In this way I would have to go some way to make sure that tab 1 gets your message and Tab 2 gets your message.

If I do this:

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

The msg field stays in the url and I did not want it.

    
asked by anonymous 25.11.2015 / 00:41

1 answer

2

Come on, I did not understand 100% what you want ... I'll try:

  

I know that TempData has its "life" held until it is   used in View. However, if I do in two different controllers,   the same identified TempData, I'm killing and over writing   them.

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

  

If the user is using 2 browser tabs, he can issue the   wrong message.

Each time he executes a URL he executes an action, the same goes for RedirectToAction , the action that will receive the request will be processed and the case explained above substitution 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   problem with multi tabs. I also did not want to go through URLs.

This part was not clear on what the multi-tab problem would be. I think if you use session will solve your problem, because whatever is placed within session in Action 1 will have the value retained when it is directed to Action 2 and the view that Action 2 call can have access to information.

        public ActionResult Index()
        {

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

In this code it will execute the Action About and the About view can display the session

@Session["message"]

Update

        public ActionResult Index(string mensagem)
        {

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

If you use session it will keep a message by tab ... Pass a code to the control and return in session the message, it will work.

    
25.11.2015 / 01:19