简体   繁体   中英

Interrupting calling data-url-action in Razor View from javascript

In application we are handling button actions by putting action name and controller name in data-url-action attribute in Razor Views like below:

<button type="button" class="btn" title="Add" data-url-action="@Url.Action("Add", "Home")">
    <span class="glyphicon glyphicon-plus" aria-hidden="true"></span>
</button>

And it works perfectly, but now I want to allow or not allow calling that action from controller. In my javascript I have function:

function setButtonsAccessibility(control) {
    if (!checkControl(control) {
        control.onclick = (function (event) {
            debugger;
            event.preventDefault();
        });
    }
}

That function catch onClick event before action from controller is called, but I have no idea how to interrupt it. That onclick function is attached when my requirements are fullfiled and here I want to prevent firing action.

I was considering to override some Razor function which recognize clicking on button with completed data-url-action as equal redirecting to pointed action, but I have no information how to find that function. Is there any possibility to solve that issue in this way or maybe is simpler solution?

event.preventDefault();

Calling mentioned above function does not solve problem.

I'm looking for generic solution, I'm not able to make hundreds changes in views - that's why I want to solve it by script.

You can try using jquery event.stopPropagation() (assuming that you're using jquery).

More details: https://api.jquery.com/event.stoppropagation/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM