zrobiłem pomocnika dla ułatwienia ponownego użycia klasy
Helper metodę rozszerzenia
namespace Extensions {
public static class HtmlExtensions {
public static MvcHtmlString FALink(this HtmlHelper htmlHelper, string action, string controller, string link_text, string fa_class, string btn_css_classes = "", string button_id = "", object route_values = null) {
// declare the span for the glyphicon
TagBuilder span = new TagBuilder("span");
span.AddCssClass($"fa fa-{fa_class}");
span.MergeAttribute("aria-hidden", "true");
// declare the anchor tag
TagBuilder anchor = new TagBuilder("a");
// Add the href attribute to the <a> element
if (string.IsNullOrEmpty(controller) || string.IsNullOrEmpty(action))
anchor.MergeAttribute("href", "#");
else
anchor.MergeAttribute("href", new UrlHelper(HttpContext.Current.Request.RequestContext).Action(action, controller, route_values));
// Add the <span> element and the text to the <a> element
anchor.InnerHtml = $"{span} {link_text}";
anchor.AddCssClass(btn_css_classes);
anchor.GenerateId(button_id);
// Create the helper
return MvcHtmlString.Create(anchor.ToString(TagRenderMode.Normal));
}
}
}
Upewnij się zawierać nazw w Widoku więc metoda jest dostępna
Przykład użycia w widoku Razor (pozostaw obszar pusty ciąg znaków, jeśli nie używasz obszarów)
@Html.FALink("Index", "ControllerNameHere", "Back to List", "th-list", "btn btn-info", "btn_back_to_list", new {area=""})
- Chociaż używam czcionki Niesamowite można łatwo zastąpić fa z klasą glyphicon używać glyphicons startowej.
- Jeśli żaden kontroler lub działania jest przekazywana, używa # jako lokalizację łącza, więc będzie grać ładny z rozwijanej interakcji
Zobacz to: http://stackoverflow.com/questions/26174013/asp- net-actionlink-with-glyphicon-and-text-with-different-font – Petr