'How can I call an alert box or console log inside of an MVC site?
I am trying to troubleshoot someone else's MVC project and I can't repro the issue in Visual Studio. Is it possible to call a Javascript alert or console.log from different functions within the c# mvc code?
I've tried this code below by calling it this way
JavascriptOne.ConsoleLog("psobjs count - " + psobjs.Count);
and having this function inside of one of the parent classes, however it does not work and nothing outputs to the console. Does something need to be added to the Javascript code? Or is there a better way to accomplish this?
public static class JavascriptOne
{
static string scriptTag = "<script type=\"\" language=\"\">{0}</script>";
public static void ConsoleLog(string message)
{
string function = "console.log('{0}');";
string log = string.Format(GenerateCodeFromFunction(function), message);
HttpContext.Current.Response.Write(log);
}
public static void Alert(string message)
{
string function = "alert('{0}');";
string log = string.Format(GenerateCodeFromFunction(function), message);
HttpContext.Current.Response.Write(log);
}
static string GenerateCodeFromFunction(string function)
{
return string.Format(scriptTag, function);
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
