Nee*_*ati 7 c# asp.net asp.net-mvc scriptmanager code-behind
有没有其他方法可以在asp.net Web应用程序中显示来自后端的警报消息而不是这个.
ScriptManager.RegisterStartupScript(this, GetType(), "alertMessage","alert('Called from code-behind directly!');", true);
Run Code Online (Sandbox Code Playgroud)
我也包括使用System.Web.UI命名空间,但仍然使用此代码获得这两个错误:
第一个错误:
'System.Web.UI.ScriptManager.RegisterStartupScript(System.Web.UI.Page,System.Type,string,string,bool)'的最佳重载方法匹配有一些无效的参数D:\ my_backup\Demos\NewShop\NewShop\API\ProductAPIController.cs 85 17 N ewShop
第二个错误:
参数1:无法从'NewShop.API.ProductAPIController'转换为'System.Web.UI.Page'D:\ my_backup\Demos\NewShop\NewShop\API\ProductAPIController .cs 85 53 NewShop
错误消息告诉您出了什么问题。该RegisterStartupScript方法需要第一个类型为 的参数System.Web.UI.Page,该参数在 ASP.NET WebForms 中使用。this相反,您将作为第一个参数传递,它是一个Controller类,在 ASP.NET MVC 中使用!
这意味着您正在使用的代码适用于另一个 Web 架构。要控制控制器的 JavaScript 输出,最好使用Model或ViewBag. 像这样:
在你的控制器代码中
ViewBag.ShowAlert = true;
Run Code Online (Sandbox Code Playgroud)
在你看来
@if (ViewBag.ShowAlert)
{
<script>alert("(Almost) called from code-behind");</script>
}
Run Code Online (Sandbox Code Playgroud)
如果您需要完全控制渲染的脚本,请将脚本保存为 ViewBag 中的字符串,尽管绝对不建议这样做!
在你的控制器代码中
ViewBag.SomeScript = "alert('Added by the controller');";
Run Code Online (Sandbox Code Playgroud)
在你看来
@if (ViewBag.SomeScript != null)
{
<script>@Html.Raw(ViewBag.SomeScript)</script>
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1000 次 |
| 最近记录: |