javascript - jQuery 调用 Controller 的操作

标签 javascript jquery asp.net-mvc

嗨,我有一个函数,一旦调用它就应该调用 Controller 操作。但它并没有调用Action Method;你能给点建议吗

function timedCount() {
        c = c + 1;
        remaining_time = max_count - c;
        if (remaining_time == 0 && logout) {
            $('#logout_popup').modal('hide');
            // This is I am trying to achieve 
            location.href = $('Home/Logout').val();
           // window.location.href = "www.google.com/";  -- Works

        } else {
            $('#timer').html(remaining_time);
            t = setTimeout(function () { timedCount() }, 1000);
        }
    }

我正在尝试实现类似 Logout Modal 的目标

最佳答案

 location.href="Home/Logout";

为了安全起见,如果您的代码位于 Razor View 中,则应使用 Url.Action Html 帮助器生成操作方法的正确相对路径。

假设您的代码位于 Razor View 内,

location.href="@Url.Action("Logout","Home")";

如果您的代码位于外部 js 文件内(不在 razor View 内),您仍然可以使用辅助方法生成 url 并将其传递给 javascript。

一种方法是在 razor View 中保留一个 html 元素,并在 javascript 代码中使用 jQuery 读取该元素。

<input type="hidden" value="@Url.Action("Logout","Home")" id="logoutUrl" />

在你的 JavaScript 中,

location.href=$("#logoutUrl").val();

另一个(更好的)解决方案是将带有所需 url 的 javascript 设置对象传递给 javascript,如本文 How do I make JS know about the application root? 中所述。 .

关于javascript - jQuery 调用 Controller 的操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37705464/

相关文章:

javascript - 用鼠标旋转div

javascript - 如何使用 Cloud 函数从 Cloud firestore 触发器中的顶级文档访问数据?

javascript - 如何用Jquery制作从左到右的填充动画

C# MVC Razor 或 Master/Content 性能

c# - 如何使用标准 ASP.NET MVC3 网站自定义 404 和 500 错误页面?

javascript - 刷新后不会保持事件状态

javascript - 在 ng-repeat 中显示带空格的键名

javascript - 多组复选框要求每组至少有 1 个

jquery - 响应式设计中的 WordPress 下拉菜单

asp.net-mvc - 如何跟踪我的应用程序的用户而不强制他们注册?