Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to show alert message in mvc 4 controller?

I tried show a alert box in mvc controller by if-else condition.But alert box does not display.Where is my mistake ?

Controller

public ActionResult Index()
{
    int userId = Convert.ToInt32(Session["userId"].ToString());

    if (WebMatrix.WebData.WebSecurity.IsAuthenticated)
    {
        if (userId == 90043) 
        {
            return View();
        }
        else
        {
            TempData["Message"] = "You are not authorized.";
            return RedirectToAction("Index", "Home");
        }
    }
    else
    {
        return RedirectToAction("Index", "Home");
    }
}
like image 449
user3107343 Avatar asked Mar 26 '14 09:03

user3107343


People also ask

How do I show messages in alert box?

Alert Box. Use the alert() function to display a message to the user that requires their attention. This alert box will have the OK button to close the alert box. The alert() function takes a paramter of any type e.g., string, number, boolean etc.

How do you return a message from a controller?

We can return the custom message from controller by throwing exception and handling it at client side using the ActionFailure event of the Grid. Grid Rendering Code. 2. Handle the returned message in the ActionFailure event of the Grid.

How do you show ViewBag message in view?

It shows on the page "@Html. Raw(ViewBag. Message)" or "@ViewBag. Message", not the actually error message.


4 Answers

Use this:

return JavaScript(alert("Hello this is an alert"));

or:

return Content("<script language='javascript' type='text/javascript'>alert('Thanks for Feedback!');</script>");
like image 24
Jeetendra Singh Negi Avatar answered Sep 19 '22 08:09

Jeetendra Singh Negi


TempData["msg"] = "<script>alert('Change succesfully');</script>";
@Html.Raw(TempData["msg"])
like image 121
vicky Avatar answered Sep 20 '22 08:09

vicky


You cannot show an alert from a controller. There is one way communication from the client to the server.The server can therefore not tell the client to do anything. The client requests and the server gives a response.

You therefore need to use javascript when the response returns to show a messagebox of some sort.

OR

using jquery on the button that calls the controller action

<script>
 $(document).ready(function(){
  $("#submitButton").on("click",function()
  {
   alert('Your Message');
  });

});
<script>
like image 45
Murdock Avatar answered Sep 21 '22 08:09

Murdock


It is not possible to display alerts from the controller. Because MVC views and controllers are entirely separated from each other. You can only display information in the view only. So it is required to pass the information to be displayed from controller to view by using either ViewBag, ViewData or TempData. If you are trying to display the content stored in TempData["Message"], It is possible to perform in the view page by adding few javascript lines.

<script>
  alert(@TempData["Message"]);
</script>
like image 31
BPX Avatar answered Sep 20 '22 08:09

BPX