Skip to content Skip to sidebar Skip to footer

Returning View After Using An An Alert Script Within The Controller

This is a question out of interest in managing JavaScript within the C# code, rather than discussion on whether this is a good design. I started experimenting with creating an aler

Solution 1:

You could utilize the TempData if you really want to make your program flow, but something came in my mind which will work without using TempData like this:

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

Now when next time the action will be called message would have value 2 and it will skip displaying alert and will move forward to do other things you need.

I hope it give you idea what i am trying to say.

Solution 2:

I'm not sure if I understand what you're looking for exactly, but is this what you want?

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>");
        elsereturn Content("<script language='javascript' type='text/javascript'>alert('indefiend Message');</script>");
    }
}

Post a Comment for "Returning View After Using An An Alert Script Within The Controller"