繁体   English   中英

如何通过jQuery在控制器或全局变量上设置属性?

[英]How can I set a property on a Controller, or Global Variable, from jQuery?

我的ASP MVC 5网站的NavBar元素中有一个复选框。 每个页面上都使用该复选框的状态来确定网格是否会显示除活动记录之外的非活动记录。

我有一个带有静态道具的静态类,我尝试使用jQuery从_Layout.cshtml中写入该静态道具,以便我的每个控制器都可以在Index()方法上检查该道具。

我尝试了下面的方法,但是后来我意识到它不能正常工作,因为一个是客户端,另一个是服务器。

那么,有没有办法做到这一点或类似的事情?

_Layout.cshtml(html)

<div class="nav pull-right checkbox navbar-btn">
                @Html.CheckBox("ShowInactive", false) Show Inactive
</div>

_Layout.cshtml(JS,非常底部)

<script type="text/javascript">
    $(document).ready(function () {
        $("#ShowInactive").prop("checked", @HttpContext.Current.Application["ShowInactive"]);
        $("#ShowInactive").click(function () {
            if (this.checked) {
                @HttpContext.Current.Application["ShowInactive"] = true;
            }
            else
            {
                @HttpContext.Current.Application["ShowInactive"] = false;
            }
            //This is to make the grid reload after the checkbox is changed.
            location.reload();
        });
    });
</script>

GlobalVariables.cs

public static class GlobalVariables
{
    public static bool ShowInactive
    {
        get { return (bool)HttpContext.Current.Application["ShowInactive"]; }
        set { HttpContext.Current.Application["ShowInactive"] = value; }
    }
}

用这个 :

document.getElementById('ShowInactive').checked = true;

要么

$('#ShowInactive').attr('checked', 'checked');

要么

$("#ShowInactive").prop("checked", true);
$(document).ready(function () {

$(“#ShowInactive”)。prop(“ checked”,true); });

暂无
暂无

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

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