简体   繁体   English

如何在 MVC4 C# 中编写 javascript?

[英]How to write javascript in MVC4 C#?

I am working in mvc 4, I have same action name with multiple view, so i cant write javascript in all pages, Simple write the javascript in action result in c# only.我在 mvc 4 中工作,我有多个视图的相同动作名称,所以我不能在所有页面中写 javascript,简单地写 javascript 只在 c# 中。

Actually i am trying like below, but its troughs an error,实际上我正在尝试如下,但它的低谷是一个错误,

     ScriptManager.RegisterStartupScript(this, this.GetType(), "script", "swal({ title: 'Dear User', text: " + TempData["MACNotice"] + ", type: 'warning',}, function () { var zip_file_path = window.location.href + '/MACSetting.zip'; var zip_file_name = 'MAC Setting'; var a = document.createElement('a'); document.body.appendChild(a); a.style = 'display: none'; a.href = zip_file_path; a.download = zip_file_name; a.click(); document.body.removeChild(a);}););", true);

Error be like:-错误如下: -

Argument 1: cannot convert from 'ParentLogins.Controllers.SigninController' to 'System.Web.UI.Control'参数 1:无法从 'ParentLogins.Controllers.SigninController' 转换为 'System.Web.UI.Control'

Argument 1: cannot convert from 'ParentLogins.Controllers.SigninController' to 'System.Web.UI.Page'参数 1:无法从 'ParentLogins.Controllers.SigninController' 转换为 'System.Web.UI.Page'

Please help me, because the lot of views are there if any possible in c# let me know, or any other options are there for write javascript, not write in all views.请帮助我,因为如果 c# 中有很多视图,请告诉我,或者有任何其他选项可以写 javascript,而不是写在所有视图中。

I think there is no way to add script in the Mvc project.cs file.我认为没有办法在 Mvc project.cs 文件中添加脚本。

Indeed, ScriptManager and just about all the script-related APIs on the Page class are not supported in MVC View Pages.实际上,MVC 视图页面不支持 ScriptManager 以及页面 class 上几乎所有与脚本相关的 API。

If you want script in a View Page, you can just place the script code there directly - no need to call any helper methods.如果您想在视图页面中使用脚本,您可以直接将脚本代码放在那里 - 无需调用任何辅助方法。

Origin 起源

And you can call your javascript function in the.cshtml file.你可以在.cshtml文件中调用你的javascript function。

eg.例如。

<script type="text/javascript">
  $(document).ready(function (){ 
    alert("WORK");
  });
</script>
HttpContext.Response.Write("<script type='text/javascript'> alert('hello');</script>");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM