【问题标题】:How to hide partial view from MVC in specific websites如何在特定网站中隐藏 MVC 的部分视图
【发布时间】:2019-01-02 00:34:05
【问题描述】:

我在 MVC 中使用局部视图在 GeneralLayout 中显示登录表单,但我想在特定网站上隐藏此登录表单。我没有找到合适的解决方案,从这里尝试了很多例子:

@if (window.location.href == 'Index/Map' || window.location.href == 'Index/Game') {
    $("#PartialView").hide();
}

或者这个:

var pathname = window.location.pathname;
console.log(pathname);
switch (pathname) {
    case "Index/Map":
        $("#PartialView").hide();
        break;
    case "Index/Game":
        $("#PartialView").hide();
        break;

我的代码如下所示:

<div class="main-content" id="HideButtons">

            <a id="logo" href="@Url.Action("Index", "Index")"></a>
            @if (PlayerSession.AppPlayerSession.IsLogged)
            {
                Html.RenderPartial("LogOnForm");
            }
            else
            {
                <button class="button" id="logIn">Log In</button>
            }
        </div>

【问题讨论】:

  • 这应该从后端处理,而不是从 javascript。我建议创建一个 HtmlExtension 来决定哪些操作结果应该显示或隐藏登录按钮,并使用 Razor 添加一个 if 逻辑
  • 您不是在检查网站,而是在检查单个网站上的页面。请具体说明您的意思,并注意您使用的词语。

标签: javascript html model-view-controller partial-views


【解决方案1】:

您可以从此代码中使用隐藏和显示登录/注销部分:

@if(User.Authenticated)
{ Html.RenderPartial("LogOnForm");}

并显示部分:

 @if(!User.Authenticated)
{ Html.RenderPartial("LoginForm");}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-07-31
    • 2015-03-03
    • 2019-10-15
    • 1970-01-01
    • 2016-01-04
    • 2011-06-03
    • 2012-03-25
    相关资源
    最近更新 更多