简体   繁体   中英

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.

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'

Argument 1: cannot convert from 'ParentLogins.Controllers.SigninController' to '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.

I think there is no way to add script in the Mvc project.cs file.

Indeed, ScriptManager and just about all the script-related APIs on the Page class are not supported in MVC View Pages.

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.

eg.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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