views:

114

answers:

2

how to insert image in html.actionlink - asp.net mvc?

i did it so, but it doesnt works.

<a href="<%= Html.ActionLink("search", "Search", new { searchText = "txtSearch" }, null); %>">
        <img alt="searchPage" style="vertical-align: middle;" height="17px"
            src="../../Stylesheets/search.PNG" title="search" />

+1  A: 

You are completely misusing the ActionLink helper. The helper actually produces the whole <a href=".." ></a> tag.

What you really want to use in this case (apart from your own written helper) is this:

<a href="<%= Url.Action("Search", new { searchText = "txtSearch" }) %>">
    <img alt="searchPage" style="vertical-align: middle;" height="17px"
        src="../../Stylesheets/search.PNG" title="search" />
</a>
Trimack
your solution doesnt works at all
Ragims
+1  A: 

i have create a helper for this solution. So u can't include image to actionLink. But with this helper class u can simply add anchor with image to view.

using System; using System.Text; using System.Web.Mvc; using System.Web.Mvc.Html; using System.Web.Routing; using System.Web; using System.Security.Policy;

namespace Helpers
{
    public static class ActionWithImageHelper
    {
        public static string AnchorIm(this HtmlHelper helper)
        {

            var builder = new TagBuilder("img");
            var link = helper.ActionLink("[replaceInnerHTML]", "replaceAction").ToHtmlString();


                builder.MergeAttribute("src", "<imagePath>");
                builder.MergeAttribute("alt", "<altText>");

                var renderedLink = link.Replace("replaceAction", "<>");
                link = renderedLink;


            return link.Replace("[replaceInnerHTML]",builder.ToString(TagRenderMode.SelfClosing));
        }

    }
}

good luck

AEMLoviji