简体   繁体   English

将HTML代码放入html helper生成的标记中

[英]Putting Html code in html helper generated tag

I have this Html code : 我有这个HTML代码:

 <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false">Espace propriétaire du terrain <span class="caret"></span></a>

I'd like to use html helper instead of using a tag 我想使用的HTML帮助,而不是使用a标签

  @Html.ActionLink("Espace propriétaire du terrain ", "About", "Home", null, new { @class = "dropdown", data_toggle = "dropdown", role = "button", aria_expanded = "false" })<span class="caret"></span>

the problem is that the span tag become outside the a tag ie in the first code I get the two tags inline but in the second it is placed vertically. 问题是span标签变成a标签之外,即在第一个代码中,我将两个标签内联,但是在第二个代码中,它是垂直放置的。

So, How can I change my code to replace the first html code by another using Html.ActionLink ? 因此,如何使用Html.ActionLink更改代码以将第一个html代码替换为另一个html代码?

而不是使用Html.ActionLink可以渲染通过URL Url.Actionhref锚标记的属性,因为你不能纳入内部的任何HTML ActionLink本身。

<a href="@Url.Action("About", "Home")" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false">Espace propriétaire du terrain <span class="caret"></span></a>

Create a custom extension method to behave the way you want it to. 创建一个自定义扩展方法,使其表现出所需的方式。

Reference: http://blog.syntaxc4.net/post/2010/10/09/How-To-Create-Custom-MVC-Extension-Methods.aspx 参考: http : //blog.syntaxc4.net/post/2010/10/09/How-To-Create-Custom-MVC-Extension-Methods.aspx

Example: 例:

  public static MvcHtmlString CustomActionLink(this HtmlHelper htmlHelper, string linkText, string area, string controller, string tabMenu, string action, RouteValueDictionary routeValues, IDictionary<string, object> htmlAttributes)
    {
        routeValues.Add("area", area);
        routeValues.Add("tabMenu", tabMenu);
        return htmlHelper.ActionLink(linkText, actionName, controllerName, routeValues, htmlAttributes);
    }

Code Src: Create a custom ActionLink 代码源: 创建自定义ActionLink

It is not possible per se. 本身是不可能的。 ActionLink will always encode the given text, so you cannot add the span inside the link description. ActionLink将始终对给定的文本进行编码,因此您不能在链接描述内添加跨度。 And putting it after is not the same. 而且放在后面是不一样的。

You could probably use CSS rules to make the a tag behave like the span tag does in this case. 在这种情况下,您可能会使用CSS规则来使标签的行为类似于span标签。

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

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