<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>ITmeze &#187; actionlink</title>
	<atom:link href="http://itmeze.com/tag/actionlink/feed/" rel="self" type="application/rss+xml" />
	<link>http://itmeze.com</link>
	<description>IT world served like Cyprus meze</description>
	<lastBuildDate>Thu, 07 Apr 2011 09:55:26 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Getting ugly with BeginActionLink helper!</title>
		<link>http://itmeze.com/2010/06/25/getting-ugly-with-beginactionlink-helper/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=getting-ugly-with-beginactionlink-helper</link>
		<comments>http://itmeze.com/2010/06/25/getting-ugly-with-beginactionlink-helper/#comments</comments>
		<pubDate>Sat, 26 Jun 2010 00:13:41 +0000</pubDate>
		<dc:creator>ITmeze</dc:creator>
				<category><![CDATA[web development]]></category>
		<category><![CDATA[actionlink]]></category>
		<category><![CDATA[asp.net-mvc]]></category>
		<category><![CDATA[htmlhelpers]]></category>
		<category><![CDATA[web]]></category>

		<guid isPermaLink="false">http://itmeze.com/?p=271</guid>
		<description><![CDATA[Have You ever had such a moment when the most stupid and trivial solution turns out to be the ONLY ONE that is logically suitable at the moment? When I had such a feeling today&#8230;. I am working on a module for one of huge web portals (not to mention which one). Part of that [...]
No related posts.]]></description>
			<content:encoded><![CDATA[<p>Have You ever had such a moment when the most stupid and trivial solution turns out to be the ONLY ONE that is logically suitable at the moment? When I had such a feeling today&#8230;.<br />
I am working on a module for one of huge web portals (not to mention which one). Part of that module is only accessible to users with administration permissions &#8211; as typical as it can be. The reason why I am mentioning that is to highlight the fact that at this specific part performance does not matter so much. Anyway, my talk to one of designers (Friday, half an hour before &#8216;the beginning of the weekend&#8217;):<br />
<strong> </strong></p>
<p><strong>Designer: </strong>I am going to spend whole weekend on those Views&#8230;</p>
<p><strong>Me: </strong>Yeah&#8230; S*** happens&#8230;.</p>
<p><strong>Designer: </strong>Those &#8216;Html.ActionLink&#8217; that You use everywhere, how do I put something inside, you know, images, spans, staff like that&#8230;</p>
<p><strong>Me: </strong>Argh&#8230; Hm&#8230;..</p>
<p>I suddenly realize that before I leave the office this guy needs an extension method: <strong>BeginActionLink </strong>like right now!. The similar one that is used for Forms elements (BeginForm). Then I started to code&#8230; Have You ever realized that ActionLink has roughly 12 overloads!!!! That would take hours to prepare BeginActionLink for most of them.</p>
<p><a href="http://itmeze.com/wp-content/uploads/2010/06/idea-bulb.jpg"><img class="alignleft size-thumbnail wp-image-275" style="margin: 10px;" title="idea" src="http://itmeze.com/wp-content/uploads/2010/06/idea-bulb-150x150.jpg" alt="idea bulb" width="150" height="150" /></a></p>
<p>When You are blocked, what You do? Me too, I started &#8216;googling&#8217;. Nothing simple and smart enough. Except 200 lines of code with tons of overloads&#8230; argh&#8230;</p>
<p>But then, completely unexpectedly &#8230;.</p>
<p>My moment of genius!</p>
<p>Don&#8217;t laugh! For people over &#8230; some age &#8230; that things do not happen offen! <img src='http://itmeze.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>There are solutions that developers are ashamed of, but they simply do their job:</p>
<pre class="brush: csharp; title: ; notranslate">public static string TrimEndTag(this MvcHtmlString htmlString, string tagName = &quot;a&quot;)
        {
            var endTag = new TagBuilder(tagName).ToString(TagRenderMode.EndTag); 

            var tagString = htmlString.ToString();

            var endTagIndex = tagString.LastIndexOf(endTag);

            if(endTagIndex &gt; 0)
                tagString = tagString.Remove(endTagIndex).TrimEnd();

            return tagString;
        }

        public static string ToEndTag(this HtmlHelper html, string tagName)
        {
            return new TagBuilder(tagName).ToString(TagRenderMode.EndTag);
        }</pre>
<p>I am simply removing end tag from ActionLink returned string! As simple as that! So all that is left for my buddy to do is to use it like that:</p>
<pre class="brush: csharp; title: ; notranslate">&lt;%= Html.ActionLink(&quot; &quot;, &quot;testAction&quot;, &quot;testController&quot;, new { id = 4 }, new {}).TrimEndTag() %&gt;

        images, spans, text, whatever...

    &lt;%= Html.ToEndTag(&quot;a&quot;) %&gt;</pre>
<p>Wuala! Ugly, So ugly, but at the same time I am preserving original methods for ActionLinks and I couldn&#8217;t figure out better solution&#8230;.</p>
<p><a class="a2a_dd a2a_target addtoany_share_save" href="http://www.addtoany.com/share_save#url=http%3A%2F%2Fitmeze.com%2F2010%2F06%2F25%2Fgetting-ugly-with-beginactionlink-helper%2F&amp;title=Getting%20ugly%20with%20BeginActionLink%20helper%21" id="wpa2a_2"><img src="http://itmeze.com/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share"/></a></p><p>No related posts.</p>]]></content:encoded>
			<wfw:commentRss>http://itmeze.com/2010/06/25/getting-ugly-with-beginactionlink-helper/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

