shkipper
shkipper

Reputation: 1413

ASP.NET MVC + Javascript (calling c# code with javascript variables)

is it possible to call c# functions with javascript parameters (from javascript)?

i.e.

<script>

    function someFunction()
    {
       var data = "123";
       var test ="<% AppHelper.GetSomething("+data+"); %>";
    }
</script>

Thank You !

Upvotes: 1

Views: 1462

Answers (3)

VinnyG
VinnyG

Reputation: 6901

What you want to do exactly because with jQuery it's very easy to do a call to a controller that return some HTML or data that you can use in javascript. I could give you an exemple if you give more details of what you are trying to do.

Upvotes: 0

anthonyv
anthonyv

Reputation: 2465

You could do it but you would have to use an ajax call to hit a URL endpoint which will execute an action. Once the action has been triggered you can do what ever you want in c#.

Upvotes: 2

peirix
peirix

Reputation: 37771

No, because ASP.NET code is run and compiled before you get to the javascript. If you want to communicate with the server using javascript, you need to use something like AJAX.

Or you might have a look at server side javascript, to see if that's something you can use.

Upvotes: 3

Related Questions