MVC Custom Tags for html.actionlink plus the ability to support image links

Source: Internet
Author: User
Tags actionlink

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.