How to add font awesome icon in Custom HTML Helper in ASP.Net MVC 5 application -
i want use font awesome icons in custom html helper not able so.
below attempt:
html helper
public static mvchtmlstring menulink(this htmlhelper htmlhelper, string linktext, string actionname, string controllername, object routevalues, object htmlattributes) { var currentaction = htmlhelper.viewcontext.routedata.getrequiredstring("action"); var currentcontroller = htmlhelper.viewcontext.routedata.getrequiredstring("controller"); var currentarea = htmlhelper.viewcontext.routedata.datatokens["area"]; var builder = new tagbuilder("li") { innerhtml = htmlhelper.actionlink(linktext, actionname, controllername, routevalues, htmlattributes).tohtmlstring() }; if (string.equals(controllername, currentcontroller, stringcomparison.currentcultureignorecase) && string.equals(actionname, currentaction, stringcomparison.currentcultureignorecase)) builder.addcssclass("active"); //builder.addcssclass("btn"); return new mvchtmlstring(builder.tostring()); } }
custom action link
@html.menulink("resume center", "index", "resume", null, new { @class = "btn btn-block"})
i modified above link not work expected. non working example below.
@html.menulink("<i class='fa fa - file'></i> resume center", "index", "resume", null, new { @class = "btn btn-block"})
original html gets rendered custom helper:
<li class="active"> <a class="btn btn-block" href="/resume">resume center</a> </li>
i want like
<li class="active"> <a class="btn btn-block" href="/resume"> <i class="fa fa-file"></i>resume center </a> </li>
please me.
actionlink()
generates <a>
tag. need replace innerhtml = htmlhelper.actionlink(...)
own code manually build html.
public static mvchtmlstring menulink(this htmlhelper htmlhelper, string linktext, string actionname, string controllername, object routevalues, object htmlattributes) { var currentaction = htmlhelper.viewcontext.routedata.getrequiredstring("action"); var currentcontroller = htmlhelper.viewcontext.routedata.getrequiredstring("controller"); var currentarea = htmlhelper.viewcontext.routedata.datatokens["area"]; // build icon , display text elements stringbuilder innerhtml = new stringbuilder(); tagbuilder icon = new tagbuilder("i"); icon.addcssclass("fa fa-file"); innerhtml.append(icon.tostring()); tagbuilder span = new tagbuilder("span"); span.innerhtml = linktext; innerhtml.append(span.tostring()); // build link tagbuilder link = new tagbuilder("a"); urlhelper urlhelper = new urlhelper(htmlhelper.viewcontext.requestcontext); string url = urlhelper.action(actionname, controllername, routevalues); link.mergeattribute("href", url); link.mergeattributes(new routevaluedictionary(htmlattributes)); link.innerhtml = innerhtml.tostring(); // build li element tagbuilder li = new tagbuilder("li"); li.innerhtml = link.tostring(); if (string.equals(controllername, currentcontroller, stringcomparison.currentcultureignorecase) && string.equals(actionname, currentaction, stringcomparison.currentcultureignorecase)) { li.addcssclass("active"); } // return html return new mvchtmlstring(li.tostring()); } }
Comments
Post a Comment