How to use the C # syntax in 'onclick'

0

I declared a variable in view :

@{
   int index = 0;
}

And I want to change it through onclick :

@for (var b = 0; b < cont; b++)
{                                               
    <li> <a onclick="@(Index = b)">Endereço @(b + 1)</a> </li>
} 
    
asked by anonymous 09.01.2018 / 12:44

1 answer

2

You can not do this directly, what runs on the client has nothing to do with what runs on the server, they are different code bases, they are even different languages. It's like you want to delete an email from your friend, even if you allow it, it would be insecure.

What can be done is to have the client ask the server to do what they want. You have to send a message through the browser and the code on the server will do what it should.

You can do this in a number of ways, one of which is AJAX , which . I already I answered an example for PHP, but in JavaScript it's the same thing .

More on the subject .

    
09.01.2018 / 12:59