Reverse lookup After using warning script in controller

This is a question of interest in managing JavaScript inside C # code, not a discussion of whether this is a good design.

I started experimenting with creating a warning from inside the controller using this answer . I understand that common practice does not use JS in the controller.

If I create a warning inside the controller, how can I control the flow of the program to then return the view. Upon return, a warning prevents progress from displaying the view.

The first method pauses the code in DoSomething:

public ActionResult DoSomething()
{
    // code to get User
    if(User.Role == someRole)
    {
        return Content("<script language='javascript' type='text/javascript'>alert('Merchant on Hold');</script>");
     }
    // More code
}

public ActionResult Dashboard()
{
    // Do things
}

The second method pauses the code in your account.

public ActionResult DoSomething()
{
    // code to get User
    if(User.Role == someRole)
    {
        return RedirectToAction("Dashboard", "AppUser", new { message = 1 });
    }
    // More code
}

public ActionResult Dashboard(int? message)
{
    if(message == 1)
        return Content("<script language='javascript' type='text/javascript'>alert('Merchant on Hold');</script>");
    // Do things
}

, , , .

, , , .

.

, , #.

+4
2

TempData, , - , TempData :

public ActionResult Dashboard(int? message)
{
    if(message == 1)
        return Content(@"<script language='javascript' type='text/javascript'>
                         alert('Merchant on Hold');
                         window.location.href='/AppUser/Dashboard?message=2'
                         </script>
                      ");
    // Do things
}

, , , 2, , , .

, , .

+1

, , , , ?

public ActionResult DoSomething()
{ 
     HttpCookie cookie = new HttpCookie("ShowAlert");
     cookie.Value = "1";
     this.ControllerContext.HttpContext.Response.Cookies.Add(cookie);
     return RedirectToAction("Dashboard", "Home", new { message = 1 });
}

public ActionResult Dashboard(int? message)
{
    if (!this.ControllerContext.HttpContext.Request.Cookies.AllKeys.Contains("ShowAlert"))
    {
        return View("Index");
    }

        HttpCookie cookie = this.ControllerContext.HttpContext.Request.Cookies["ShowAlert"];
        cookie.Expires = DateTime.Now.AddDays(-1);
        this.ControllerContext.HttpContext.Response.Cookies.Add(cookie);
        if (message == 1)
            return Content("<script language='javascript' type='text/javascript'>alert('Merchant on Hold');</script>");
        else
            return Content("<script language='javascript' type='text/javascript'>alert('indefiend Message');</script>");
    }
}
+1

All Articles