【问题标题】:Whats the use of htmlattributes of html.action methodhtml.action方法的html属性有什么用
【发布时间】:2012-12-17 14:58:19
【问题描述】:

我是 MVC 新手,在 asp.net 中使用 MVC 2.0。

这是stylesheet1.css的内容:

.h1{color:Red;}

这是display.aspx的内容:

< head runat="server">
<## Heading ##link href="../../Content/StyleSheet1.css" rel="stylesheet" type="text/css" />
< /head> 
<## body>
<## h1 class="h1">Hiiiiiiiiiiiiiiiii  <## /h1>
<## /body>

这是index.aspx的内容:

##<%= Html.ActionLink("Move to display.aspx","display","home",new {id=1}, new 
{@class="h1"}) %>

##<%= Html.ActionLink("Move to display.aspx", "display")%>

我的问题是 htmlattributes 参数在 html.actionlink 方法上的用途是什么,因为我发现上面的链接给出了相同的结果。

【问题讨论】:

    标签: asp.net-mvc html.actionlink


    【解决方案1】:

    如果您进行搜索,Google 上有大量文章,例如:

    Use CSS on Html.ActionLink

    ActionLink htmlAttributes

    MVC HTML Helpers

    希望这些对你有用。

    【讨论】:

    • 我只是想知道我们是否可以在不使用 html.actionlink 方法的 htmlattribute 的情况下获得结果,那么为什么将其添加到 MVC 框架中,请仔细阅读我的问题----@Gaz
    • 如果你能看到 index.aspx 的内容,有两个 html.action 方法具有不同的参数并且都给出相同的结果......所以我只是想知道什么是作用html.action方法的html属性............对我来说似乎没用......希望你会知道......@Gaz
    【解决方案2】:

    您的操作链接可能正在呈现如下内容:

    <a href="home/display/1" class="h1">Move to display.aspx</a>
    
    <a href="display">Move to display.aspx</a>
    

    它们之间的区别是第一个将 css 类设置为 h1 样式类(不是标签),然后将 1 作为 url 上的 id 参数传递给家庭控制器上的显示操作方法。第二个是显示动作方法(如果它在家庭控制器的视图中,它将转到家庭控制器的显示动作方法),它没有设置任何 css 类。两个操作链接都将文本呈现为Move to display.aspx。如果您没有 h1 类设置的 css,则这种情况下的 htmlAttributes 没有区别。

    你能做什么

    首先,在您的 css 文件中创建一个具有有效名称的样式表,如下所示:

    .display {
        color: blue;
        /* other css properties*/
    }
    

    在您看来,使用设置在 class 属性上的此样式表呈现链接:

    <%=Html.ActionLink("Move To Display", "Display", "Home", new { id = 1 }, new { @class="display" }) %>
    

    关于 htmlAttributes

    htmlAttributes 参数是一个对象,其中包含要为元素设置的 HTML 属性。例如,如果您想在输出链接中添加一个 css 类,您可以通过此参数添加此属性,例如 reltitletabindex,JavaScript 事件例如 onclick 等。

    Html.ActionLink 方法中有很多重载。您不需要传递此参数,但如果您需要具有此参数的重载,您可以只传递 null 并且不会在您的 html 中输出任何内容。如果你这样做:

    @Html.ActionLink("Text Link", "Action", "Controller", new { id = 5 }, new { @class = "button", title = "Some Title Content", rel = "10" })
    

    如果将是以下签名方法:

    public static MvcHtmlString ActionLink(
        this HtmlHelper htmlHelper,
        string linkText,
        string actionName,
        Object routeValues,
        Object htmlAttributes
    )
    

    它会渲染

    <a href="Controller/Action/5" class="button" title="Some Title Content" rel="10">Text Link</a>
    

    【讨论】:

    • 我很抱歉,但只是没有得到我想要的......无论如何......你能告诉我如何获得蓝色的 display.aspx 的内容...... .我的意思是这应该工作
    • 我编辑了我的 anwser。看一下,我想你不会在 action link 方法渲染的这些链接之间得到任何区别。
    • 或者这只是添加锚标签属性
    • 我再次编辑你能做什么。 htmlAttributes like my anwser add in html output html 的属性,如类、标题、rel 等,关于 html 标签而不是服务器端。
    • 好的,我尝试更改 display.aspx 内容的颜色,但没有成功。这是 display.aspx
      hiiiiiiiiiiiiiiiii
      的内容,在 index.aspx 我做了 但是 display.aspx 内容的颜色不会改变,而是锚点的颜色标签变为绿色
    【解决方案3】:

    您分配给链接的类h1 被浏览器的默认样式表覆盖。 (如果您想了解详细信息,请阅读css specificity)。

    您需要做的就是将您的 css 类定义更改为

    a.h1{color:Red;}
    

    (注意定义前面的a

    现在这个链接中的文字:

    <%= Html.ActionLink("Move to display.aspx","display","home",new {id=1}, new 
    {@class="h1"}) %>
    

    会是红色的。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-04-29
      • 2011-05-23
      • 1970-01-01
      • 1970-01-01
      • 2010-11-14
      相关资源
      最近更新 更多