<?xml version="1.0" encoding="utf-8"?>
<rss xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:pingback="http://madskills.com/public/xml/rss/module/pingback/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" version="2.0">
  <channel>
    <title>Mirko's Blog - ASP.NET MVC</title>
    <link>http://blogs.aspiant.com/Mirko/</link>
    <description>For You and Your Silicone Pet</description>
    <language>en-us</language>
    <copyright>Mirko Geffken</copyright>
    <lastBuildDate>Mon, 15 Jun 2009 08:59:28 GMT</lastBuildDate>
    <generator>newtelligence dasBlog 2.3.9074.18820</generator>
    <managingEditor>Mirko.Geffken@aspiant.com</managingEditor>
    <webMaster>Mirko.Geffken@aspiant.com</webMaster>
    <item>
      <trackback:ping>http://blogs.aspiant.com/Mirko/Trackback.aspx?guid=0bc618ee-cce8-4f3a-8269-b7d38f790e5f</trackback:ping>
      <pingback:server>http://blogs.aspiant.com/Mirko/pingback.aspx</pingback:server>
      <pingback:target>http://blogs.aspiant.com/Mirko/PermaLink,guid,0bc618ee-cce8-4f3a-8269-b7d38f790e5f.aspx</pingback:target>
      <dc:creator>Mirko Geffken</dc:creator>
      <wfw:comment>http://blogs.aspiant.com/Mirko/CommentView,guid,0bc618ee-cce8-4f3a-8269-b7d38f790e5f.aspx</wfw:comment>
      <wfw:commentRss>http://blogs.aspiant.com/Mirko/SyndicationService.asmx/GetEntryCommentsRss?guid=0bc618ee-cce8-4f3a-8269-b7d38f790e5f</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
Today I had the desire to change some of the ActionLinks we have been using from boring
text to exciting images (which meant using my image collection as I have no talent
whatsoever in drawing anything). Unfortunately I was unable to locate a respective
method on the existing HtmlHelper. 
</p>
        <p>
I am a big fan of the strongly-typed variety of these methods, which are, as of today,
only available in the MVC Futures (available at <a href="http://aspnet.codeplex.com/Release/ProjectReleases.aspx?ReleaseId=24471" target="_blank">CodePlex</a>).
</p>
        <p>
In my hunt for a efficient (lazy) copy-and-paste solution I came across a thread on
Stack Overflow with a comment by <a href="http://stackoverflow.com/questions/989005/make-an-html-actionlink-around-an-image-in-asp-net-mvc" target="_blank">eu-ge-ne</a>.
His code does a nice job of getting an image produced, but did not get me the desired
strongly-typed implementation I was seeking.
</p>
        <p>
His version was this (with a minor fix and my personal dislike of var replaced):<br /></p>
        <pre class="brush: csharp; gutter: false;">public static string ActionLinkWithImage(this HtmlHelper html, string imgSrc, string actionName)
{
    UrlHelper urlHelper = new UrlHelper(html.ViewContext.RequestContext); 
    string imgUrl = urlHelper.Content(imgSrc); 
    TagBuilder imgTagBuilder = new TagBuilder("img"); 
    imgTagBuilder.MergeAttribute("src", imgUrl); 
    string img = imgTagBuilder.ToString(TagRenderMode.Normal); 
    string url = urlHelper.Action(actionName); 
    
    TagBuilder tagBuilder = new TagBuilder("a")
                                {
                                    InnerHtml = img
                                }; 
    
    tagBuilder.MergeAttribute("href", url); 
    return tagBuilder.ToString(TagRenderMode.Normal);
}
</pre>
        <p>
The following does almost the same, but does it strongly-typed. 
<br /></p>
        <pre class="brush: csharp; gutter: false;">public static class HtmlHelperExtensions
{
    public static string ActionLinkWithImage&lt;TController&gt;(this HtmlHelper html, Expression&lt;Action&lt;TController&gt;&gt; action, string imgSrc) where TController: Controller
    {
        UrlHelper urlHelper = new UrlHelper(html.ViewContext.RequestContext); 
        string imgUrl = urlHelper.Content(imgSrc); 
        TagBuilder imgTagBuilder = new TagBuilder("img"); 
        imgTagBuilder.MergeAttribute("src", imgUrl); 
        string img = imgTagBuilder.ToString(TagRenderMode.Normal); 
        TagBuilder tagBuilder = new TagBuilder("a")
                                    {
                                        InnerHtml = img
                                    }; 
        tagBuilder.MergeAttribute("href", LinkBuilder.BuildUrlFromExpression(html.ViewContext.RequestContext, html.RouteCollection, action)); 
        return tagBuilder.ToString(TagRenderMode.Normal);
    }

    public static string ActionLinkWithImage&lt;TController&gt;(this HtmlHelper html, Expression&lt;Action&lt;TController&gt;&gt; action, string imgSrc, object imageAttributes, object linkAttributes) where TController : Controller
    {
        UrlHelper urlHelper = new UrlHelper(html.ViewContext.RequestContext);
        string imgUrl = urlHelper.Content(imgSrc);
        TagBuilder imgTagBuilder = new TagBuilder("img");
        imgTagBuilder.MergeAttribute("src", imgUrl);

        imgTagBuilder.MergeAttributes(new RouteValueDictionary(imageAttributes));

        string img = imgTagBuilder.ToString(TagRenderMode.Normal);
        
        TagBuilder tagBuilder = new TagBuilder("a")
        {
            InnerHtml = img
        };

        tagBuilder.MergeAttributes(new RouteValueDictionary(linkAttributes));
        tagBuilder.MergeAttribute("href", LinkBuilder.BuildUrlFromExpression(html.ViewContext.RequestContext, html.RouteCollection, action));
        return tagBuilder.ToString(TagRenderMode.Normal);
    }
}
</pre>
        <p>
An Example of consumption on your page would then be:<br /></p>
        <pre class="brush: csharp; gutter: false;">&lt;%= Html.ActionLinkWithImage&lt;FooController&gt;(fc =&gt; fc.Edit(deal.ID), "~/Content/Images/edit_16.png", new { style = "border-style: none;", alt = "Edit Foo", title = "Edit Deal" }, null)%&gt;
</pre>
        <p>
Hope this helps someone with the same predicament (Someone feel free to incorporate
into the Futures assembly :)) 
</p>
        <img width="0" height="0" src="http://blogs.aspiant.com/Mirko/aggbug.ashx?id=0bc618ee-cce8-4f3a-8269-b7d38f790e5f" />
      </body>
      <title>ASP.NET MVC Strongly-Typed ActionLink with Images</title>
      <guid isPermaLink="false">http://blogs.aspiant.com/Mirko/PermaLink,guid,0bc618ee-cce8-4f3a-8269-b7d38f790e5f.aspx</guid>
      <link>http://blogs.aspiant.com/Mirko/2009/06/15/ASPNETMVCStronglyTypedActionLinkWithImages.aspx</link>
      <pubDate>Mon, 15 Jun 2009 08:59:28 GMT</pubDate>
      <description>&lt;p&gt;
Today I had the desire to change some of the ActionLinks we have been using from boring
text to exciting images (which meant using my image collection as I have no talent
whatsoever in drawing anything). Unfortunately I was unable to locate a respective
method on the existing HtmlHelper. 
&lt;/p&gt;
&lt;p&gt;
I am a big fan of the strongly-typed variety of these methods, which are, as of today,
only available in the MVC Futures (available at &lt;a href="http://aspnet.codeplex.com/Release/ProjectReleases.aspx?ReleaseId=24471" target=_blank&gt;CodePlex&lt;/a&gt;).
&lt;/p&gt;
&lt;p&gt;
In my hunt for a efficient (lazy) copy-and-paste solution I came across a thread on
Stack Overflow with a comment by &lt;a href="http://stackoverflow.com/questions/989005/make-an-html-actionlink-around-an-image-in-asp-net-mvc" target=_blank&gt;eu-ge-ne&lt;/a&gt;.
His code does a nice job of getting an image produced, but did not get me the desired
strongly-typed implementation I was seeking.
&lt;/p&gt;
&lt;p&gt;
His version was this (with a minor fix and my personal dislike of var replaced):&lt;br&gt;
&lt;/p&gt;
&lt;pre class="brush: csharp; gutter: false;"&gt;public static string ActionLinkWithImage(this HtmlHelper html, string imgSrc, string actionName)
{
    UrlHelper urlHelper = new UrlHelper(html.ViewContext.RequestContext); 
    string imgUrl = urlHelper.Content(imgSrc); 
    TagBuilder imgTagBuilder = new TagBuilder("img"); 
    imgTagBuilder.MergeAttribute("src", imgUrl); 
    string img = imgTagBuilder.ToString(TagRenderMode.Normal); 
    string url = urlHelper.Action(actionName); 
    
    TagBuilder tagBuilder = new TagBuilder("a")
                                {
                                    InnerHtml = img
                                }; 
    
    tagBuilder.MergeAttribute("href", url); 
    return tagBuilder.ToString(TagRenderMode.Normal);
}
&lt;/pre&gt;
&lt;p&gt;
The following does almost the same, but does it strongly-typed. 
&lt;br&gt;
&lt;/p&gt;
&lt;pre class="brush: csharp; gutter: false;"&gt;public static class HtmlHelperExtensions
{
    public static string ActionLinkWithImage&amp;lt;TController&amp;gt;(this HtmlHelper html, Expression&amp;lt;Action&amp;lt;TController&amp;gt;&amp;gt; action, string imgSrc) where TController: Controller
    {
        UrlHelper urlHelper = new UrlHelper(html.ViewContext.RequestContext); 
        string imgUrl = urlHelper.Content(imgSrc); 
        TagBuilder imgTagBuilder = new TagBuilder("img"); 
        imgTagBuilder.MergeAttribute("src", imgUrl); 
        string img = imgTagBuilder.ToString(TagRenderMode.Normal); 
        TagBuilder tagBuilder = new TagBuilder("a")
                                    {
                                        InnerHtml = img
                                    }; 
        tagBuilder.MergeAttribute("href", LinkBuilder.BuildUrlFromExpression(html.ViewContext.RequestContext, html.RouteCollection, action)); 
        return tagBuilder.ToString(TagRenderMode.Normal);
    }

    public static string ActionLinkWithImage&amp;lt;TController&amp;gt;(this HtmlHelper html, Expression&amp;lt;Action&amp;lt;TController&amp;gt;&amp;gt; action, string imgSrc, object imageAttributes, object linkAttributes) where TController : Controller
    {
        UrlHelper urlHelper = new UrlHelper(html.ViewContext.RequestContext);
        string imgUrl = urlHelper.Content(imgSrc);
        TagBuilder imgTagBuilder = new TagBuilder("img");
        imgTagBuilder.MergeAttribute("src", imgUrl);

        imgTagBuilder.MergeAttributes(new RouteValueDictionary(imageAttributes));

        string img = imgTagBuilder.ToString(TagRenderMode.Normal);
        
        TagBuilder tagBuilder = new TagBuilder("a")
        {
            InnerHtml = img
        };

        tagBuilder.MergeAttributes(new RouteValueDictionary(linkAttributes));
        tagBuilder.MergeAttribute("href", LinkBuilder.BuildUrlFromExpression(html.ViewContext.RequestContext, html.RouteCollection, action));
        return tagBuilder.ToString(TagRenderMode.Normal);
    }
}
&lt;/pre&gt;
&lt;p&gt;
An Example of consumption on your page would then be:&lt;br&gt;
&lt;/p&gt;
&lt;pre class="brush: csharp; gutter: false;"&gt;&amp;lt;%= Html.ActionLinkWithImage&amp;lt;FooController&amp;gt;(fc =&amp;gt; fc.Edit(deal.ID), "~/Content/Images/edit_16.png", new { style = "border-style: none;", alt = "Edit Foo", title = "Edit Deal" }, null)%&amp;gt;
&lt;/pre&gt;
&lt;p&gt;
Hope this helps someone with the same predicament (Someone feel free to incorporate
into the Futures assembly :)) 
&lt;/p&gt;
&lt;img width="0" height="0" src="http://blogs.aspiant.com/Mirko/aggbug.ashx?id=0bc618ee-cce8-4f3a-8269-b7d38f790e5f" /&gt;</description>
      <comments>http://blogs.aspiant.com/Mirko/CommentView,guid,0bc618ee-cce8-4f3a-8269-b7d38f790e5f.aspx</comments>
      <category>ASP.NET MVC</category>
    </item>
    <item>
      <trackback:ping>http://blogs.aspiant.com/Mirko/Trackback.aspx?guid=3ae92447-1115-4eb9-9194-75925575a190</trackback:ping>
      <pingback:server>http://blogs.aspiant.com/Mirko/pingback.aspx</pingback:server>
      <pingback:target>http://blogs.aspiant.com/Mirko/PermaLink,guid,3ae92447-1115-4eb9-9194-75925575a190.aspx</pingback:target>
      <dc:creator>Mirko Geffken</dc:creator>
      <wfw:comment>http://blogs.aspiant.com/Mirko/CommentView,guid,3ae92447-1115-4eb9-9194-75925575a190.aspx</wfw:comment>
      <wfw:commentRss>http://blogs.aspiant.com/Mirko/SyndicationService.asmx/GetEntryCommentsRss?guid=3ae92447-1115-4eb9-9194-75925575a190</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
Today I tried to put a relatively simple form together and was hoping to be done in
a few minutes. Included on the form is an optional field asking for a monetary value.
The type in the database is money and using LINQ-to-SQL the type is represented as
a decimal.
</p>
        <p>
All was well until the user entered a value that decimals generally don’t like to
be converted into. Let’s assume ‘aaa’ for the time being. The result I was greeted
with was this:<br /></p>
        <pre class="brush: plain; gutter: false;">The model of type 'Foo' was not successfully updated.
</pre>My
desire for ASP.NET MVC to handle this for me and put a nice validation message into
the ModelState was just not happening. 
<br /><br />
My original code did this: <pre class="brush: csharp; gutter: false;">Foo foo = _modelRepository.FindFoo(id);<br /><br />
UpdateModel(foo); </pre><p></p>
The relatively simple fix is: <pre class="brush: csharp; gutter: false;">Foo foo = _modelRepository.FindFoo(id);

UpdateModel(foo, "SomeStringField", "SomeOtherStringField");

if (!TryUpdateModel(foo, "TheDecimalField"))
{
    ModelState.AddModelError("TheDecimalField", "Does that look like a number to you?");
}
</pre><img width="0" height="0" src="http://blogs.aspiant.com/Mirko/aggbug.ashx?id=3ae92447-1115-4eb9-9194-75925575a190" /></body>
      <title>ASP.NET MVC Form Validation Error with Decimal Parsing</title>
      <guid isPermaLink="false">http://blogs.aspiant.com/Mirko/PermaLink,guid,3ae92447-1115-4eb9-9194-75925575a190.aspx</guid>
      <link>http://blogs.aspiant.com/Mirko/2009/06/13/ASPNETMVCFormValidationErrorWithDecimalParsing.aspx</link>
      <pubDate>Sat, 13 Jun 2009 20:20:23 GMT</pubDate>
      <description>&lt;p&gt;
Today I tried to put a relatively simple form together and was hoping to be done in
a few minutes. Included on the form is an optional field asking for a monetary value.
The type in the database is money and using LINQ-to-SQL the type is represented as
a decimal.
&lt;/p&gt;
&lt;p&gt;
All was well until the user entered a value that decimals generally don’t like to
be converted into. Let’s assume ‘aaa’ for the time being. The result I was greeted
with was this:&lt;br&gt;
&lt;/p&gt;
&lt;pre class="brush: plain; gutter: false;"&gt;The model of type 'Foo' was not successfully updated.
&lt;/pre&gt;My
desire for ASP.NET MVC to handle this for me and put a nice validation message into
the ModelState was just not happening. 
&lt;br&gt;
&lt;br&gt;
My original code did this: &lt;pre class="brush: csharp; gutter: false;"&gt;Foo foo = _modelRepository.FindFoo(id);&lt;br&gt;
&lt;br&gt;
UpdateModel(foo); &lt;/pre&gt;
&lt;p&gt;
&lt;/p&gt;
The relatively simple fix is: &lt;pre class="brush: csharp; gutter: false;"&gt;Foo foo = _modelRepository.FindFoo(id);

UpdateModel(foo, "SomeStringField", "SomeOtherStringField");

if (!TryUpdateModel(foo, "TheDecimalField"))
{
    ModelState.AddModelError("TheDecimalField", "Does that look like a number to you?");
}
&lt;/pre&gt;&lt;img width="0" height="0" src="http://blogs.aspiant.com/Mirko/aggbug.ashx?id=3ae92447-1115-4eb9-9194-75925575a190" /&gt;</description>
      <comments>http://blogs.aspiant.com/Mirko/CommentView,guid,3ae92447-1115-4eb9-9194-75925575a190.aspx</comments>
      <category>ASP.NET MVC</category>
    </item>
  </channel>
</rss>