Link to home
Start Free TrialLog in
Avatar of Michael Sterling
Michael SterlingFlag for United States of America

asked on

How do send a javascript/jQuery alert from my contoller in MVC5?

I need to send a simple javascript / jQuery alert message from my controller in MVC5. How do I do this? I have the code below in place, but the alert is not firing, nothing happens when the line of code with the script in it is hit.

				if (manager.AddedServicesList == null)
				{
					Response.Write("<script>alert('NO SERVICES ADDED');</script>");
					return RedirectToAction("Category", manager);
				}
				else
					return RedirectToAction("GroupDetails", manager);

Open in new window


This works, but then I just get a blank page. I need to get back to/stay on/in the view where I'm currently at.

return Content("<script language='javascript' type='text/javascript'>alert('Hello world!');</script>");

Open in new window

Avatar of leakim971
leakim971
Flag of Guadeloupe image

using the ViewBag :
https://docs.microsoft.com/en-us/dotnet/api/system.web.mvc.controllerbase.viewbag?view=aspnet-mvc-5.2
				if (manager.AddedServicesList == null)
				{
					Viewbag.myscript = "<script>alert('NO SERVICES ADDED');</script>";
					return RedirectToAction("Category", manager);
				}
				else
					return RedirectToAction("GroupDetails", manager);

Open in new window


And use the following in your view :
@Html.Raw(Viewbag.myscript);

Open in new window

Avatar of Michael Sterling

ASKER

If this is the 2nd time my reply posts, I'll remove one of them.

@leakim971: Thank you, that suggestion is close. It did work, in the sense that, it did keep the user on the page, which I needed to do. However, the alert message never showed. I stepped through the code in my view and in the controller and it definitely hits the code that you recommended in both files, but the alert message doesn't display. Any guesses as to why?
Open the Chrome developer console and open your page, do you thing to see if the debugger command halt
Viewbag.myscript = "<script>debugger;alert('NO SERVICES ADDED');</script>";

Open in new window


Also I don't like the RedirectToAction...
What should I use instead of RedirectToAction? I'm not sure what you're asking me to do with the chrome debugger??? This line of code:

Viewbag.myscript = "<script>debugger;alert('NO SERVICES ADDED');</script>";

Open in new window



 is in my controller.
it's in your controller but it's supposed to be in your view after page update (@Html.Raw(Viewbag.myscript);)
so the goal is to be sure the code run fin and alert is not disabled in your browser for any reason (so we don't lose time for something so stupid)
Understood. Putting it directly into my view did work. Is there a way to put some boolean logic around it? Or was this just a test to make sure that alert was not disabled in my browser?
Putting it directly into my view did work.

I just did a simple test and it work fine.
Just want to be sure, you corrected the type, it's ViewBag and not viewbag
ASKER CERTIFIED SOLUTION
Avatar of leakim971
leakim971
Flag of Guadeloupe image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial