none
Set variable value with HTML element RRS feed

  • Question

  • Hello!
    Is there any way to set an ASP.NET variable value with an HTML element?

    For example: 
    <a id="_test"> Click here </a>

    @{

         var x = HTML.ELEMENT("_test");

    }

    I want to get "onclick" event and make my webpage load different content into a <div> tag.

    Thanks for helping xD 

    Thursday, February 15, 2018 6:51 PM

All replies

  • You can't. and the reason is that they do not "live" in the same time. The Razor variables are "Server side variables" and they don't exist anymore after the page was sent to the "Client side".

    When the server get a request for a view, it creates the view with only HTML, CSS and Javascript code. No C# code is left, it's all get "translated" to the client side languages.

    you can read javascript variable using @: keyword

    But it would be possible if one were used in place of the variable in @html.Hidden field. As in this example.

    @Html.Hidden("myVar", 0);

    set the field per script:

    <script>

    function setMyValue(value) {

         $('#myVar').val(value);      

    }

    </script>


    Thanks

    Lavanya

    Monday, November 19, 2018 7:52 AM