简体   繁体   English

从javascript中断Razor View中的调用data-url-action

[英]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: 在应用程序中,我们通过将操作名称和控制器名称放在Razor Views中的data-url-action属性中来处理按钮操作,如下所示:

<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: 在我的JavaScript中我有功能:

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. 该函数在调用控制器操作之前捕获onClick事件,但我不知道如何中断它。 That onclick function is attached when my requirements are fullfiled and here I want to prevent firing action. 当我的要求被完整填充时附加了onclick功能,在这里我想阻止触发操作。

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. 我正在考虑覆盖一些Razor函数,该函数识别单击按钮并将完成的data-url-action同等重定向到指向的动作,但我没有信息如何找到该函数。 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). 您可以尝试使用jquery event.stopPropagation() (假设您使用的是jquery)。

More details: https://api.jquery.com/event.stoppropagation/ 更多细节: https//api.jquery.com/event.stoppropagation/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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