I have an event which is thrown when a button on the keyboard is pressed. I want this event to be called for only one view. For now, in my Layout view I have:
<body onkeydown="theMethod()">
But this is getting called in all the pages of the application. How to call the only for one view? The views don’t have body tag.
Bind the event dynamically in the one view that you want it to be fired.
In the view that you do want key events on the body to be handled, add code like this to the view’s markup:
This is a jQuery example (you can do without jQuery but it’s included with MVC scaffolding and is elegant for situations like this).
Abstracting event handling from inline declarations is a good practice and makes scenarios like this easier to handle.