<?xml version="1.0" encoding="utf-8"?>
<feed xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xml:lang="en-us" xmlns="http://www.w3.org/2005/Atom">
  <title>Mirko's Blog</title>
  <link rel="alternate" type="text/html" href="http://blogs.aspiant.com/Mirko/" />
  <link rel="self" href="http://blogs.aspiant.com/Mirko/SyndicationService.asmx/GetAtom" />
  <icon>favicon.ico</icon>
  <updated>2009-06-15T06:33:05.140234-04:00</updated>
  <author>
    <name>Mirko Geffken</name>
  </author>
  <subtitle>For You and Your Silicone Pet</subtitle>
  <id>http://blogs.aspiant.com/Mirko/</id>
  <generator uri="http://dasblog.info/" version="2.3.9074.18820">DasBlog</generator>
  <entry>
    <title>ASP.NET MVC Strongly-Typed ActionLink with Images</title>
    <link rel="alternate" type="text/html" href="http://blogs.aspiant.com/Mirko/2009/06/15/ASPNETMVCStronglyTypedActionLinkWithImages.aspx" />
    <id>http://blogs.aspiant.com/Mirko/PermaLink,guid,0bc618ee-cce8-4f3a-8269-b7d38f790e5f.aspx</id>
    <published>2009-06-15T04:59:28.629-04:00</published>
    <updated>2009-06-15T05:37:57.3203692-04:00</updated>
    <category term="ASP.NET MVC" label="ASP.NET MVC" scheme="http://blogs.aspiant.com/Mirko/CategoryView,category,ASPNETMVC.aspx" />
    <author>
      <name>Mirko Geffken</name>
    </author>
    <content type="xhtml">
      <div 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" />
      </div>
    </content>
  </entry>
  <entry>
    <title>ASP.NET MVC Form Validation Error with Decimal Parsing</title>
    <link rel="alternate" type="text/html" href="http://blogs.aspiant.com/Mirko/2009/06/13/ASPNETMVCFormValidationErrorWithDecimalParsing.aspx" />
    <id>http://blogs.aspiant.com/Mirko/PermaLink,guid,3ae92447-1115-4eb9-9194-75925575a190.aspx</id>
    <published>2009-06-13T16:20:23.441-04:00</published>
    <updated>2009-06-15T06:33:05.140234-04:00</updated>
    <category term="ASP.NET MVC" label="ASP.NET MVC" scheme="http://blogs.aspiant.com/Mirko/CategoryView,category,ASPNETMVC.aspx" />
    <author>
      <name>Mirko Geffken</name>
    </author>
    <content type="xhtml">
      <div 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" /></div>
    </content>
  </entry>
</feed>