Set Viewbag before Redirect

C#asp.net Mvc

C# Problem Overview


Is it possible to set the ViewBag before I call a redirection?

I want something like:

@ViewBag.Message="MyMessage";
RedirectToAction("MyAction");

C# Solutions


Solution 1 - C#

When you use redirection, you shall not use ViewBag, but TempData

public ActionResult Action1 () {
 TempData["shortMessage"] = "MyMessage";
 return RedirectToAction("Action2");
}

public ActionResult Action2 () {
 //now I can populate my ViewBag (if I want to) with the TempData["shortMessage"] content
  ViewBag.Message = TempData["shortMessage"].ToString();
  return View();
}

Solution 2 - C#

You can use the TempData in this situation. Here is some explanation for the ViewBag, ViewData and TempData.

Solution 3 - C#

I did like this..and its working for me... here I'm changing password and on success I want to set success message to viewbag to display on view..

    public ActionResult ChangePass()
    {
        ChangePassword CP = new ChangePassword();
        if (TempData["status"] != null)
        {
            ViewBag.Status = "Success";
            TempData.Remove("status");
        }
        return View(CP);
    }

    [HttpPost]
    public ActionResult ChangePass(ChangePassword obj)
    {
        if (ModelState.IsValid)
        {
            int pid = Session.GetDataFromSession<int>("ssnPersonnelID");
            PersonnelMaster PM = db.PersonnelMasters.SingleOrDefault(x => x.PersonnelID == pid);

            PM.Password = obj.NewPassword;
            PM.Mdate = DateTime.Now;
            db.SaveChanges();

            TempData["status"] = "Success";
            return RedirectToAction("ChangePass");
        }
        
        return View(obj);
    }

Solution 4 - C#

Taken from here

>Summary > >The ViewData and ViewBag objects give you ways to access those extra pieces of data that go alongside your model, however for more complex data, you can move up to the ViewModel. TempData, on the other hand, is geared specifically for working with data on HTTP redirects, so remember to be cautious when using TempData.

Solution 5 - C#

Or you can use Session for alternative:

Session["message"] = "MyMessage";
RedirectToAction("MyAction");

and then call it whenever you need.

UPDATE

Also, as what @James said in his comment, it would be safe to nullify or clear the value of that specific session after you use it in order to avoid unwanted junk data or outdated value.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestiondanielView Question on Stackoverflow
Solution 1 - C#Raphaël AlthausView Answer on Stackoverflow
Solution 2 - C#laszlokiss88View Answer on Stackoverflow
Solution 3 - C#RAVI VAGHELAView Answer on Stackoverflow
Solution 4 - C#Paul ZahraView Answer on Stackoverflow
Solution 5 - C#Jon PView Answer on Stackoverflow