MVC'de image link nasıl yapılır?

June 3, 2009 at 7:45 PMoozturk

MVC'de alıştıktan sonra insan vazgeçemiyor sürekli link için action, controller, id gibi özellikler ile link yaratmaya başlıyor. Eh bu durumdan sonra resimlere de link vermek isteyince kendi html helper fonksiyonumuzu yazmak zorunda kalıyoruz. Aşağıdaki örneği aynen kullanabilirsiniz.

        public static string ImageLink(this HtmlHelper helper,
                              string action,
                              string controller,
                              object routeValues,
                              string imageURL,
                              string alternateText,
                              object linkHtmlAttributes,
                                string style,
                              object imageHtmlAttributes)
        {
        // Create an instance of UrlHelper
           UrlHelper url = new UrlHelper(helper.ViewContext.RequestContext);
        //Create image tag builder
            TagBuilder imageBuilder = new TagBuilder("img");

            if( !string.IsNullOrEmpty( style ) )
                imageBuilder.MergeAttribute( "style", style );

        // Add image attributes
            imageBuilder.MergeAttribute("src", imageURL);
            imageBuilder.MergeAttribute("alt", alternateText);
            imageBuilder.MergeAttributes(new RouteValueDictionary(imageHtmlAttributes));
        //' Create link tag builder
            TagBuilder linkBuilder = new TagBuilder("a");
        //' Add attributes
            linkBuilder.MergeAttribute("href", url.Action(action, controller, new RouteValueDictionary(routeValues)));
            linkBuilder.InnerHtml = imageBuilder.ToString(TagRenderMode.SelfClosing);
            linkBuilder.MergeAttributes(new RouteValueDictionary(linkHtmlAttributes));
        //' Render tag
            return linkBuilder.ToString( TagRenderMode.Normal );
        }

Bu fonksiyonun kullanımına dair bir örnek vereyim: 

<%= Html.ImageLink("Detail", "Firma", new { id = firma.Firmaadi }, "/content/images/a.jpg", firma.Firmadi, null, "margin-left:10px; border:1px solid #CCC", null) %> 

 

Kolay gelsin!

Posted in: MVC

Tags: ,

Comments (10) -

I prefer to look on the brighter side of things which I'm sure you can appreciate.

Reply

I found this post while surfing the net some random stuff. Thanks for sharing will be sure to follow this blog regularly and will email this post to my friends.

Reply

Well that makes 2 things I learned today.  This and google now has a ton of greasemonkey scripts which I just found out.  You learn something every day?  I got more than what I bargained for! hahaha.

Reply

I don't give a damn for a man that can only spell a word one way.

Reply

Who is the admin of this page? I'd like to trade links.  Looking at the amount of traffic that MVC'de image link nasil yapilir? gets and it'd be great for me to exchange links.  Got to keep ahead of the competition and all.  And, most of my links come from sites just like here.

Reply

You should wite a blog post about a recommendation of sites like MVC'de image link nasil yapilir?. I came across your blog two days ago when I was googling, however I am not really into the blog thing. I don't think it's because I do not enjoy blogs, but more than likely due to the fact that I'm a little ignorant to them.  But blogengine is cool.

Reply

I love viewing this blogs articles, keep up the amazing work!

Reply

I just came across this blog I was I am getting a 404 page not found when trying to access other posts I hope it gets fixed. Thanks

Reply

When are you going to post again? You really entertain me!

Reply

As a Novice, I am continually scouting around  for posts that will assist me .. Thanks a lot how does this compare to amoebas.other-games.games1234.net/playgame.aspx

Reply

Add comment

  Country flag

biuquote
  • Comment
  • Preview
Loading