Michael Sterling
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.
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.
if (manager.AddedServicesList == null)
{
Response.Write("<script>alert('NO SERVICES ADDED');</script>");
return RedirectToAction("Category", manager);
}
else
return RedirectToAction("GroupDetails", manager);
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>");
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?
@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
Also I don't like the RedirectToAction...
Viewbag.myscript = "<script>debugger;alert('NO SERVICES ADDED');</script>";
Also I don't like the RedirectToAction...
ASKER
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:
is in my controller.
Viewbag.myscript = "<script>debugger;alert('NO SERVICES ADDED');</script>";
is in my controller.
it's in your controller but it's supposed to be in your view after page update (@Html.Raw(Viewbag.myscrip t);)
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)
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)
ASKER
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
https://docs.microsoft.com/en-us/dotnet/api/system.web.mvc.controllerbase.viewbag?view=aspnet-mvc-5.2
Open in new window
And use the following in your view :
Open in new window