How to use C# syntax in onclick

Asked

Viewed 104 times

0

I declared a variable in view:

@{
   int index = 0;
}

And I want to change it through a onclick:

@for (var b = 0; b < cont; b++)
{                                               
    <li> <a onclick="@(Index = b)">Endereço @(b + 1)</a> </li>
} 
  • Dude, what exactly do you need to do? Why do you want to change the value of the variable? I believe your solution is confused and can be improved depending on what you need..

  • Because right below I have this: <input name="NMLOGCRE" value="@(Model.Addresses[Index]. Split('*')[1])" >

  • @Andrénakamura The answer solved your question? Do you think you can accept it? See [tour] if you don’t know how you do it. This would help a lot to indicate that the solution was useful for you. You can also vote on any question or answer you find useful on the entire site (when you have 15 points).

1 answer

2

You can’t do this directly, what runs on the client has nothing to do with what runs on the server, they’re different code bases, they’re even different languages. It’s like you want to delete an email message from your friend, even if you allowed it, it would be unsafe.

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

It is possible to do this in many ways, one of them is with AJAX, what is probably the most appropriate in this case. Already I answered an example for PHP, but in Javascript it’s the same thing.

More on the subject.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.