I have two questions:
Html.ActionLink()
in an MVC view (actually, this is Site.Mast
It's very simple.
If you want to have something like a glyphicon icon and then "Wish List",
<span class="glyphicon-heart"></span> @Html.ActionLink("Wish List (0)", "Index", "Home")
Here is an uber expansion of @tvanfosson's answer. I was inspired by it and decide to make it more generic.
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);
}
}
Please try below Code that may help you.
@Html.ActionLink(" SignIn", "Login", "Account", routeValues: null, htmlAttributes: new { id = "loginLink" ,**@class="glyphicon glyphicon-log-in"** })
A custom HtmlHelper extension is another option. Note: ParameterDictionary is my own type. You could substitute a RouteValueDictionary but you'd have to construct it differently.
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();
}
Just use Url.Action
instead of Html.ActionLink
:
<li id="home_nav"><a href="<%= Url.Action("ActionName") %>"><span>Span text</span></a></li>