Click here to Skip to main content
16,004,974 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Razor
@{string test = string.Empty;}

@{test=
      <text>
      $('#__RequestVerificationToken').val()
        </text>
      ;}


What I have tried:

Just want to fetch value from Element and pass it to c# object
Posted
Updated 2-Mar-16 20:41pm

1 solution

No, you can't assign javascript variables to C# variable. Because razor variable are server variables but javascript variables are client side variable.

When the server gets a request for a view, it creates the view with only HTML, CSS and Javascript code. The Javascript code do exist when the view is still on the server, it will be executed by the browser only (Client side again). It is meaning less.

Yes, you can assign server variables to javacript:
JavaScript
<script> var myJsVariable = '@ViewBag.MyVariable' </script>
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900