我有两个问题:
- 我想知道
Html.ActionLink()
在 MVC 视图中使用时如何不显示链接文本(实际上,这是Site.Master
)。
没有不允许链接文本的重载版本,当我尝试只传入一个空白string
时,编译器告诉我它需要一个非空字符串。
我怎样才能解决这个问题?
我需要将
<span>
标签放在锚标签中,但它不适用于Html.ActionLink();
. 我想看到以下输出:跨度文本
如何将标签放在 ASP.NET MVC 中的锚标签内?
我有两个问题:
Html.ActionLink()
在 MVC 视图中使用时如何不显示链接文本(实际上,这是Site.Master
)。没有不允许链接文本的重载版本,当我尝试只传入一个空白string
时,编译器告诉我它需要一个非空字符串。
我怎样才能解决这个问题?
我需要将<span>
标签放在锚标签中,但它不适用于Html.ActionLink();
. 我想看到以下输出:
跨度文本
如何将标签放在 ASP.NET MVC 中的锚标签内?
您可以通过 Url.Action 呈现 url,而不是使用 Html.ActionLink
<a href="<%= Url.Action("Index", "Home") %>"><span>Text</span></a>
<a href="@Url.Action("Index", "Home")"><span>Text</span></a>
并做一个空白的网址,你可以
<a href="<%= Url.Action("Index", "Home") %>"></a>
<a href="@Url.Action("Index", "Home")"></a>
自定义 HtmlHelper 扩展是另一种选择。 注意: ParameterDictionary 是我自己的类型。您可以替换 RouteValueDictionary 但您必须以不同的方式构造它。
public static string ActionLinkSpan( this HtmlHelper helper, string linkText, string actionName, string controllerName, object htmlAttributes )
{
TagBuilder spanBuilder = new TagBuilder( "span" );
spanBuilder.InnerHtml = linkText;
return BuildNestedAnchor( spanBuilder.ToString(), string.Format( "/{0}/{1}", controllerName, actionName ), htmlAttributes );
}
private static string BuildNestedAnchor( string innerHtml, string url, object htmlAttributes )
{
TagBuilder anchorBuilder = new TagBuilder( "a" );
anchorBuilder.Attributes.Add( "href", url );
anchorBuilder.MergeAttributes( new ParameterDictionary( htmlAttributes ) );
anchorBuilder.InnerHtml = innerHtml;
return anchorBuilder.ToString();
}
如果您需要使用 ajax 或手动创建链接时无法使用的某些功能(使用标签),这是(低和肮脏的)解决方法:
<%= Html.ActionLink("LinkTextToken", "ActionName", "ControllerName").ToHtmlString().Replace("LinkTextToken", "Refresh <span class='large sprite refresh'></span>")%>
您可以使用任何文本代替“LinkTextToken”,它只是被替换,重要的是它不会出现在 actionlink 内的其他任何地方。
只需使用Url.Action
而不是Html.ActionLink
:
<li id="home_nav"><a href="<%= Url.Action("ActionName") %>"><span>Span text</span></a></li>
这对我来说一直很有效。它不凌乱而且非常干净。
<a href="@Url.Action("Index", "Home")"><span>Text</span></a>
我最终得到了一个自定义扩展方法。值得注意的是,当尝试将 HTML 放置在 Anchor 对象中时,链接文本可以位于内部 HTML 的左侧或右侧。出于这个原因,我选择为左右内部 HTML 提供参数——链接文本在中间。左右内部 HTML 都是可选的。
扩展方法 ActionLinkInnerHtml:
public static MvcHtmlString ActionLinkInnerHtml(this HtmlHelper helper, string linkText, string actionName, string controllerName, RouteValueDictionary routeValues = null, IDictionary<string, object> htmlAttributes = null, string leftInnerHtml = null, string rightInnerHtml = null)
{
// CONSTRUCT THE URL
var urlHelper = new UrlHelper(helper.ViewContext.RequestContext);
var url = urlHelper.Action(actionName: actionName, controllerName: controllerName, routeValues: routeValues);
// CREATE AN ANCHOR TAG BUILDER
var builder = new TagBuilder("a");
builder.InnerHtml = string.Format("{0}{1}{2}", leftInnerHtml, linkText, rightInnerHtml);
builder.MergeAttribute(key: "href", value: url);
// ADD HTML ATTRIBUTES
builder.MergeAttributes(htmlAttributes, replaceExisting: true);
// BUILD THE STRING AND RETURN IT
var mvcHtmlString = MvcHtmlString.Create(builder.ToString());
return mvcHtmlString;
}
使用示例:
这是一个使用示例。对于这个例子,我只想要链接文本右侧的内部 html...
@Html.ActionLinkInnerHtml(
linkText: "Hello World"
, actionName: "SomethingOtherThanIndex"
, controllerName: "SomethingOtherThanHome"
, rightInnerHtml: "<span class=\"caret\" />"
)
结果:
这会产生以下 HTML ...
<a href="/SomethingOtherThanHome/SomethingOtherThanIndex">Hello World<span class="caret" /></a>
我认为这在使用引导程序和一些 glypicons 时可能很有用:
<a class="btn btn-primary"
href="<%: Url.Action("Download File", "Download",
new { id = msg.Id, distributorId = msg.DistributorId }) %>">
Download
<span class="glyphicon glyphicon-paperclip"></span>
</a>
这将显示一个 A 标签,带有指向控制器的链接,上面有一个漂亮的回形针图标来表示下载链接,并且 html 输出保持干净
这是@tvanfosson 答案的超级扩展。我受到它的启发,并决定让它更通用。
public static MvcHtmlString NestedActionLink(this HtmlHelper htmlHelper, string linkText, string actionName,
string controllerName, object routeValues = null, object htmlAttributes = null,
RouteValueDictionary childElements = null)
{
var htmlAttributesDictionary = HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes);
if (childElements != null)
{
var urlHelper = new UrlHelper(htmlHelper.ViewContext.RequestContext);
var anchorTag = new TagBuilder("a");
anchorTag.MergeAttribute("href",
routeValues == null
? urlHelper.Action(actionName, controllerName)
: urlHelper.Action(actionName, controllerName, routeValues));
anchorTag.MergeAttributes(htmlAttributesDictionary);
TagBuilder childTag = null;
if (childElements != null)
{
foreach (var childElement in childElements)
{
childTag = new TagBuilder(childElement.Key.Split('|')[0]);
object elementAttributes;
childElements.TryGetValue(childElement.Key, out elementAttributes);
var attributes = HtmlHelper.AnonymousObjectToHtmlAttributes(elementAttributes);
foreach (var attribute in attributes)
{
switch (attribute.Key)
{
case "@class":
childTag.AddCssClass(attribute.Value.ToString());
break;
case "InnerText":
childTag.SetInnerText(attribute.Value.ToString());
break;
default:
childTag.MergeAttribute(attribute.Key, attribute.Value.ToString());
break;
}
}
childTag.ToString(TagRenderMode.SelfClosing);
if (childTag != null) anchorTag.InnerHtml += childTag.ToString();
}
}
return MvcHtmlString.Create(anchorTag.ToString(TagRenderMode.Normal));
}
else
{
return htmlHelper.ActionLink(linkText, actionName, controllerName, routeValues, htmlAttributesDictionary);
}
}
这很简单。
如果你想要一个像字形图标然后是“愿望清单”的东西,
<span class="glyphicon-heart"></span> @Html.ActionLink("Wish List (0)", "Index", "Home")
请尝试以下可能对您有所帮助的代码。
@Html.ActionLink(" SignIn", "Login", "Account", routeValues: null, htmlAttributes: new { id = "loginLink" ,**@class="glyphicon glyphicon-log-in"** })
我使用引导组件的解决方案:
<a class="btn btn-primary" href="@Url.Action("resetpassword", "Account")">
<span class="glyphicon glyphicon-user"></span> Reset Password
</a>
请尝试以下代码:
@Html.Raw(@Html.ActionLink("text", "ActionName", "ControllerName", null, new { area = "Home" }).ToHtmlString().Replace("text", "<i class='fa fa-car sub-icon-mg' aria-hidden='true'></i> <span class='mini-sub-pro'>Link Text</span>"))