Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. Call JavaScript function from c# code in cshtml

Call JavaScript function from c# code in cshtml

Scheduled Pinned Locked Moved ASP.NET
csharpjavascripttools
4 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    sunsher
    wrote on last edited by
    #1

    Is it possible to call a javascript function in a cshtml, in the @{} section, and the javascript function is defined in the script section of the cshtml. Thanks

    F 1 Reply Last reply
    0
    • S sunsher

      Is it possible to call a javascript function in a cshtml, in the @{} section, and the javascript function is defined in the script section of the cshtml. Thanks

      F Offline
      F Offline
      F ES Sitecore
      wrote on last edited by
      #2

      No, your cshtml is compiled and executed as .net code on the server, the result is that it generates html, that html is then sent to the client browser to be executed and the client browser can interpret and execute javascript. So you can't run javascript in your views as the javascript doesn't yet technically exist.

      S 1 Reply Last reply
      0
      • F F ES Sitecore

        No, your cshtml is compiled and executed as .net code on the server, the result is that it generates html, that html is then sent to the client browser to be executed and the client browser can interpret and execute javascript. So you can't run javascript in your views as the javascript doesn't yet technically exist.

        S Offline
        S Offline
        sunsher
        wrote on last edited by
        #3

        I have a list passed on to the view from the controller through a viewbag. so if the viewbag has the list, then execute a javascript function that is defined in the script section of the page. what is the other option?

        F 1 Reply Last reply
        0
        • S sunsher

          I have a list passed on to the view from the controller through a viewbag. so if the viewbag has the list, then execute a javascript function that is defined in the script section of the page. what is the other option?

          F Offline
          F Offline
          F ES Sitecore
          wrote on last edited by
          #4

          <script type="text/javascript">
          function myFunc() {
          alert('Hello');
          }
          </script>

          @if (ViewBag.MyList != null)
          {
          <script>myFunc();</script>
          }

          Note the order is important, js is interpreted in order so you can't call functions that have not yet been defined.

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups