Reprint: http://blog.csdn.net/niewq/article/details/17537873
Html.ActionLink only supports text links, and now we use the C # extension method to write a html.actionlinkwithimage () feature that supports image linking.
First, create a new static class, the namespace is changed under SYSTEM.WEB.MVC, the code is as follows
usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingsystem.web;usingSYSTEM.WEB.MVC;namespaceSYSTEM.WEB.MVC { Public Static classMyhelpler { Public StaticMvchtmlstring Actionlinkwithimage ( ThisHtmlHelper HTML,stringIMGSRC,stringactionname) { varUrlhelper =Newurlhelper (HTML. Viewcontext.requestcontext); stringImgurl =urlhelper.content (IMGSRC); Tagbuilder Imgtagbuilder=NewTagbuilder ("img"); Imgtagbuilder.mergeattribute ("src", Imgurl); stringIMG =imgtagbuilder.tostring (tagrendermode.selfclosing); stringURL =urlhelper.action (ActionName); Tagbuilder Tagbuilder=NewTagbuilder ("a") {InnerHtml=img}; Tagbuilder.mergeattribute ("href", URL); return Newmvchtmlstring (tagbuilder.tostring (tagrendermode.normal)); } Public StaticMvchtmlstring Actionlinkwithimage ( ThisHtmlHelper HTML,stringIMGSRC,stringActionName,stringControllername,ObjectRoutevalue=NULL) { varUrlhelper =Newurlhelper (HTML. Viewcontext.requestcontext); stringImgurl =urlhelper.content (IMGSRC); Tagbuilder Imgtagbuilder=NewTagbuilder ("img"); Imgtagbuilder.mergeattribute ("src", Imgurl); stringIMG =imgtagbuilder.tostring (tagrendermode.selfclosing); stringURL =urlhelper.action (ActionName, Controllername, RouteValue); Tagbuilder Tagbuilder=NewTagbuilder ("a") {InnerHtml=img}; Tagbuilder.mergeattribute ("href", URL); return Newmvchtmlstring (tagbuilder.tostring (tagrendermode.normal)); } } }
Second, in view under use:
@Html. Actionlinkwithimage (Url.content ("~/content/images/index_1.gif" ) Index") @Html. Actionlinkwithimage (url.content ("~/content/images/index_2.gif "List","Admin"new { id=1})
MVC's linked picture label is complete, and can be passed in to the proposed controller, method, and incoming parameters.
MVC Custom tags, add features that support image linking to Html.ActionLink