有没有办法让 HTML.ActionLink 默认不可见?

发布于 2024-08-09 15:38:27 字数 68 浏览 4 评论 0原文

我试图在页面首次呈现时默认隐藏 HTML.ActionLink,然后根据用户所在的页面控制其可见性。有什么建议吗? 谢谢!

I am trying to hide HTML.ActionLink by default when page first renders and then control it's visiblity based on the page the the user is on. Any suggestions?
Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

━╋う一瞬間旳綻放 2024-08-16 15:38:27

您可以通过设置操作链接的 html 属性来设置操作链接的任何样式。

Html.ActionLink(
    "LinkName", 
    "Action", 
    null,
    new { @style = "display:none" });

you can set any style on the action link by setting it's html attributes.

Html.ActionLink(
    "LinkName", 
    "Action", 
    null,
    new { @style = "display:none" });
£烟消云散 2024-08-16 15:38:27

要决定隐藏或显示链接,您需要通过 Model 或 ViewData 提供此信息。我想您在母版页中有链接。

因此,第一步是为站点中的所有视图提供该信息。您可以通过创建基本控制器并覆盖一个方法来做到这一点(当然,所有控制器都应该从新控制器继承):

protected override void OnActionExecuted(ActionExecutedContext filterContext)
{
    if (filterContext.ActionResult is ViewResult) {
        ViewData["Shared-ShowTheLink"] = IsLinkeVisible(filterContext) ? "non-null" : null;
    }       
}

private bool IsLinkeVisible(ActionExecutedContext filterContext) {
    // Show on the home page only, for example
    var controllerName = filterContext.RouteData.Values["controller"];
    var actionName = filterContext.RouteData.Values["action"];

    var isHome = string.Compare(controllerName, "Home", StringComparison.InvariantCultureIgnoreCase) == 0;
    var isIndex = string.Compare(actionName, "Index", StringComparison.InvariantCultureIgnoreCase) == 0;
    return isHome && isIndex;
}

之后您需要到您的母版页(或视图)并执行类似的操作(假设 WebForms 视图引擎) ):

<% if (ViewData["Shared-ShowTheLink"] != null) { %>
    Html.ActionLink("Link Text", "Action", "Controller");
<% } %>

现在您的链接应该仅在主页/索引上可见。
要更改此设置,请根据需要修改 IsLinkeVisible 方法。

干杯。

To decide weather to hide or show the link you need to provide this information via Model or ViewData. I suppose you have the link in a master page.

So first step is to provide that information for all views in your site. You can do it by creating base controller and override a method (and of course all your controllers should inherit form the new one):

protected override void OnActionExecuted(ActionExecutedContext filterContext)
{
    if (filterContext.ActionResult is ViewResult) {
        ViewData["Shared-ShowTheLink"] = IsLinkeVisible(filterContext) ? "non-null" : null;
    }       
}

private bool IsLinkeVisible(ActionExecutedContext filterContext) {
    // Show on the home page only, for example
    var controllerName = filterContext.RouteData.Values["controller"];
    var actionName = filterContext.RouteData.Values["action"];

    var isHome = string.Compare(controllerName, "Home", StringComparison.InvariantCultureIgnoreCase) == 0;
    var isIndex = string.Compare(actionName, "Index", StringComparison.InvariantCultureIgnoreCase) == 0;
    return isHome && isIndex;
}

After that you need to to your master page (or view) and do something like this (assuming WebForms view engine):

<% if (ViewData["Shared-ShowTheLink"] != null) { %>
    Html.ActionLink("Link Text", "Action", "Controller");
<% } %>

Now your link should be visible on Home/Index only.
To change this modify IsLinkeVisible method as you need.

Cheers.

大海や 2024-08-16 15:38:27

默认情况下,使用 CSS display:none 属性呈现链接。然后使用 Javascript 将 display 切换为需要时显示链接的值。更多信息请参见此处

jQuery 提供了 show() 和 hide() 效果,允许您轻松切换元素的可见性或元素。

By default, render the link with the CSS display:none attribute. Then use Javascript to switch display to a value that will display the link when required. More information here.

jQuery provides the show() and hide() effects that allow you to easily toggle the visibility of an element or elements.

聽兲甴掵 2024-08-16 15:38:27

使用 htmlAttributes 重载创建操作链接帮助器以给它一个类

Html.ActionLink("link","link",null, new { @class=model.mystatus })

您可以从模型中的属性设置 @class 值,然后应用“display:none;”添加到 CSS 中的类或使用 jquery 将元素设置为隐藏 --> $('.statusclass').hide();

create your action link helper with the htmlAttributes overload to give it a class

Html.ActionLink("link","link",null, new { @class=model.mystatus })

You would set the @class value from a property in your model then either apply "display:none;" to the class in CSS or set the element to hidden with jquery --> $('.statusclass').hide();

江心雾 2024-08-16 15:38:27

唯一有效的方法是将 ActionLink 包装在一个跨度中,分配一个类,将其隐藏在 CSS 中,然后在 JavaScript 中显示它。由于某种原因,直接将类名分配给 ActionLink 是行不通的。

感谢大家的帮助!

The only thing that worked is wrapping the ActionLink in a span, assigning a class, hiding it in CSS, and showing it in JavaScript. For some reason assigning a class name directly to ActionLink would not work.

Thanks for everyone's help!

紫南 2024-08-16 15:38:27

这里有一个技巧:

  • 将 CSS 类添加到链接
  • 将 CSS 类添加到每个页面的 body 标记,根据需要使用尽可能多的唯一值
  • 编辑 CSS 文件以根据 body 标记类的组合显示/隐藏链接以及链接上的班级。

CSS 文件示例:

body.events .myLink,
body.about .myLink {
   display:none;
}
body.programs .myLink {
   display:inline;
}

那么您不需要对 ActionLink 做任何奇怪的事情。这并没有解决为什么你要发出一个你不想首先显示的链接的问题,但我相信你有充分的理由。

Here's a technique:

  • Add a CSS class to the link
  • Add a CSS class to the body tag of every page, use as many unique values as you need
  • Edit your CSS file to show/hide the link based on the combination of the body tag class and the class on the link.

Example CSS file:

body.events .myLink,
body.about .myLink {
   display:none;
}
body.programs .myLink {
   display:inline;
}

Then you don't need to do anything funky with the ActionLink. This doesn't address the question of why you're emitting a link that you don't want to show in the first place, but I'll trust you have a good reason for it.

两相知 2024-08-16 15:38:27

我猜您想控制 Html.ActionLink 辅助方法生成的标记的可见性,对吗?如果是,您可以按如下所示动态指定标签的类:

<%=Html.ActionLink("link", "MyAction", null, new { @class = ViewData["actionLinkClass"] })%>

在控制器操作方法中,您可以为可见或隐藏样式定义 ViewData["actionLinkClass"] 值。

I guess that you want to control the visibility of the tag generated by the Html.ActionLink helper method, is that right? If yes, you can specify dinamically the class for the tag as below:

<%=Html.ActionLink("link", "MyAction", null, new { @class = ViewData["actionLinkClass"] })%>

In your controller action method you can define the ViewData["actionLinkClass"] value for the visible or hidden style.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文