如果你想在 MVC 视图中使用 @Html.ActionLink 获得这种结果:
<a href="#"><i class="fa-editico"></i></a>
希望此代码示例对您有所帮助,我测试了此代码,一切正常。祝你好运:)
MVC 助手:
//iElementClassName = <i> - element class
public static MvcHtmlString ActionLinkCustom(this HtmlHelper htmlHelper, string iElementClassName, string action, string controller, object routeValues, object htmlAttributes)
{
var urlHelper = new UrlHelper(htmlHelper.ViewContext.RequestContext, htmlHelper.RouteCollection);
//get array of HTML attributes
var attributes = AnonymousObjectToKeyValue(htmlAttributes);
//create <a> - Tag
var anchor = new TagBuilder("a");
//add <i> tag inside in "<a> <a/>" Tag
anchor.InnerHtml = string.Format("<i class='{0}'></i>", iElementClassName);
//Make Href attribute
anchor.MergeAttribute("href", urlHelper.Action(action, controller, routeValues));
//add array of attributes
anchor.MergeAttributes(attributes, true);
return MvcHtmlString.Create(anchor.ToString());
}
//It helps to generate attribute's array
private static Dictionary<string, object> AnonymousObjectToKeyValue(object anonymousObject)
{
var dictionary = new Dictionary<string, object>();
if (anonymousObject == null) return dictionary;
foreach (PropertyDescriptor propertyDescriptor in TypeDescriptor.GetProperties(anonymousObject))
{
dictionary.Add(propertyDescriptor.Name, propertyDescriptor.GetValue(anonymousObject));
}
return dictionary;
}
MVC 视图:
//When user click edit css icon, MVC Controller gets Id and we see Alert message
//fa-edit - This is a CSS class name
@Html.ActionLinkCustom("fa-editico","ActionName", "ControllerName", new { Id = Model.Id},
new
{
title = "Edit Button",
onclick = "alert("It Works !");"
})