How to pass a viewbag with RedirectToAction

Justin :

I got a controller which assign a value to the ViewBag and that ViewBag is used in the script,

this is my code, i have no idea whats wrong, i even tried using TempData but nothing happens everytime it shows that my ViewBag is null.

public ActionResult ChechUserNP(Login dd)
{
    if (dd.userid == "admin" && dd.pwd == "admin")
    {
        return RedirectToAction("Index", "DataGridView");
    }
    else
    {
        @ViewBag.Message = "MyMessage";
        return RedirectToAction("Index", "Login");
    }
}
<script>
    debugger;
    if (@ViewBag.Message !== null) {
    }
</script>
Rion Williams :

Consider using TempData instead as it's specifically geared towards scenarios involving redirects.

TempData["Message"] = "MyMessage";
return RedirectToAction("Index", "Login");

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=301570&siteId=1