Update a viewbag (c # razor) with a javascript variable

0

Maybe what I want to do may be impossible, due to server and browser side issues, but I'd like to do the following:

<script>
    function pegarId(id) {
        $('@ViewBag.Id').value = $(this).attr("id");        
    }
</script>

Thank you in advance for any tip or help.

    
asked by anonymous 30.09.2015 / 07:04

1 answer

1

You can not change the ViewBag from Javascript since the ViewBag is a feature that works entirely on the server side.

The opposite, however, is possible: You can set a javascript variable that receives a value that will be written from the ViewBag during View rendering. Something like:

<script>
    var umaVariavel = @ViewBag.MeuValor
</script>
    
30.09.2015 / 16:17