4 Replies - 294 Views - Last Post: 03 July 2019 - 12:33 PM Rate Topic: -----

#1 BenignDesign   User is offline

  • holy shitin shishkebobs
  • member icon




Reputation: 7987
  • View blog
  • Posts: 12,406
  • Joined: 28-September 07

Calling Javascript Function from C#

Posted 03 July 2019 - 06:24 AM

I'm passing a module ID through a URL to the same page (ie, mysite.com/Module to mysite.com/Module?y=4). I'm using Javascript to get the variable passed through the URL, but I don't know how to call the Javascript function from the C#.

The Javascript:
<script>
    function getUrlVars() {
        var vars = {};
        var parts = window.location.href.replace(/[?&]+([^=&]+)=([^&]*)/gi, function (m, key, value) {
            vars[key] = value;
        });
        return vars;
    }

   function getUrlParam(parameter, defaultvalue) {
       var urlparameter = defaultvalue;
       if (window.location.href.indexOf(parameter) > -1) {
          urlparameter = getUrlVars()[parameter];
        }
        return urlparameter;
    }
</script>



The C#:
foreach (var m in Model)
{
   var moduleID = m.Module.Id;
   var modUrl = getUrlVars()["y"];
            
   <button class="accordion"><h2>@m.Module.Name</h2></button>
   if (modUrl == moduleID)
   {
       //Do something.
   } else {
       //Do something else.
   }
}



The Error Message:

Quote

The name 'getUrlVars' does not exist in the current context


Help? Please?

Is This A Good Question/Topic? 0
  • +

Replies To: Calling Javascript Function from C#

#2 andrewsw   User is online

  • never lube your breaks
  • member icon

Reputation: 6798
  • View blog
  • Posts: 28,098
  • Joined: 12-December 12

Re: Calling Javascript Function from C#

Posted 03 July 2019 - 06:41 AM

You wouldn't call JS from C#.

Are you using ASP.NET MVC or Core? The approaches differ slightly as I recall. Either way, the url is available in C#, either through model binding or something like RouteData. See here at SO for some variations.

You might even be able to do something like

public ActionResult Foo(AModel model, int y) { 


after a bit of jiggling.
Was This Post Helpful? 0
  • +
  • -

#3 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 6968
  • View blog
  • Posts: 23,684
  • Joined: 05-May 12

Re: Calling Javascript Function from C#

Posted 03 July 2019 - 06:44 AM

Yup, the Razor page can't call the Javascript directly. Recall that a Razor page is compiled into C# code that essentially uses the T4 engine. That C# code can't dynamically call the Javascript.
Was This Post Helpful? 0
  • +
  • -

#4 DarenR   User is offline

  • D.I.C Lover

Reputation: 617
  • View blog
  • Posts: 4,108
  • Joined: 12-January 10

Re: Calling Javascript Function from C#

Posted 03 July 2019 - 06:57 AM

cant you make a hidden field and set the hidden field then call that field from c# or is that not what you want to do?
Was This Post Helpful? 0
  • +
  • -

#5 modi123_1   User is offline

  • Suitor #2
  • member icon



Reputation: 15113
  • View blog
  • Posts: 60,464
  • Joined: 12-June 08

Re: Calling Javascript Function from C#

Posted 03 July 2019 - 12:33 PM

$0.02 here - your url parameters are picked up in 'routedata'. You can snag them in the C# code with that. No need to run through JS first.

https://docs.microso...etframework-4.8
https://www.tutorial...at-is-routedata
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1