<?xml version='1.0' encoding='UTF-8'?><?xml-stylesheet href="http://www.blogger.com/styles/atom.css" type="text/css"?><feed xmlns='http://www.w3.org/2005/Atom' xmlns:openSearch='http://a9.com/-/spec/opensearchrss/1.0/' xmlns:georss='http://www.georss.org/georss' xmlns:gd='http://schemas.google.com/g/2005' xmlns:thr='http://purl.org/syndication/thread/1.0'><id>tag:blogger.com,1999:blog-1171265999480681980</id><updated>2012-03-17T03:30:42.804-07:00</updated><category term='C#'/><category term='ASP.NET MVC'/><title type='text'>Treats for your Silicone Pet</title><subtitle type='html'></subtitle><link rel='http://schemas.google.com/g/2005#feed' type='application/atom+xml' href='http://mirko.blogs.aspiant.com/feeds/posts/default'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1171265999480681980/posts/default'/><link rel='alternate' type='text/html' href='http://mirko.blogs.aspiant.com/'/><link rel='hub' href='http://pubsubhubbub.appspot.com/'/><author><name>Mirko Geffken</name><uri>http://www.blogger.com/profile/08748094891809938994</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><generator version='7.00' uri='http://www.blogger.com'>Blogger</generator><openSearch:totalResults>2</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>25</openSearch:itemsPerPage><entry><id>tag:blogger.com,1999:blog-1171265999480681980.post-2615580093584870368</id><published>2009-06-15T06:51:00.000-07:00</published><updated>2011-04-01T09:04:33.020-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='ASP.NET MVC'/><category scheme='http://www.blogger.com/atom/ns#' term='C#'/><title type='text'>ASP.NET MVC Strongly-Typed ActionLink with Images</title><content type='html'>&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 an 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="code"&gt;&lt;span style="color: blue"&gt;public static string &lt;/span&gt;ActionLinkWithImage(&lt;span style="color: blue"&gt;this &lt;/span&gt;HtmlHelper html, &lt;span style="color: blue"&gt;string &lt;/span&gt;imgSrc, &lt;span style="color: blue"&gt;string &lt;/span&gt;actionName)&lt;br /&gt;{&lt;br /&gt;    UrlHelper urlHelper = &lt;span style="color: blue"&gt;new &lt;/span&gt;UrlHelper(html.ViewContext.RequestContext); &lt;br /&gt;    &lt;span style="color: blue"&gt;string &lt;/span&gt;imgUrl = urlHelper.Content(imgSrc); &lt;br /&gt;    TagBuilder imgTagBuilder = &lt;span style="color: blue"&gt;new &lt;/span&gt;TagBuilder(&lt;span style="color: #a31515"&gt;"img"&lt;/span&gt;); &lt;br /&gt;    imgTagBuilder.MergeAttribute(&lt;span style="color: #a31515"&gt;"src"&lt;/span&gt;, imgUrl); &lt;br /&gt;    &lt;span style="color: blue"&gt;string &lt;/span&gt;img = imgTagBuilder.ToString(TagRenderMode.Normal); &lt;br /&gt;    &lt;span style="color: blue"&gt;string &lt;/span&gt;url = urlHelper.Action(actionName); &lt;br /&gt;&lt;br /&gt;    TagBuilder tagBuilder = &lt;span style="color: blue"&gt;new &lt;/span&gt;TagBuilder(&lt;span style="color: #a31515"&gt;"a"&lt;/span&gt;)&lt;br /&gt;                                {&lt;br /&gt;                                    InnerHtml = img&lt;br /&gt;                                }; &lt;br /&gt;&lt;br /&gt;    tagBuilder.MergeAttribute(&lt;span style="color: #a31515"&gt;"href"&lt;/span&gt;, url); &lt;br /&gt;    &lt;span style="color: blue"&gt;return &lt;/span&gt;tagBuilder.ToString(TagRenderMode.Normal);&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;The following does almost the same, but strongly-typed.&lt;br /&gt;&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;public static class &lt;/span&gt;HtmlHelperExtensions&lt;br /&gt;{&lt;br /&gt;    &lt;span style="color: blue"&gt;public static string &lt;/span&gt;ActionLinkWithImage&amp;lt;TController&amp;gt;(&lt;span style="color: blue"&gt;this &lt;/span&gt;HtmlHelper html, Expression&amp;lt;Action&amp;lt;TController&amp;gt;&amp;gt; action, &lt;span style="color: blue"&gt;string &lt;/span&gt;imgSrc) where TController : Controller&lt;br /&gt;    {&lt;br /&gt;        UrlHelper urlHelper = &lt;span style="color: blue"&gt;new &lt;/span&gt;UrlHelper(html.ViewContext.RequestContext);&lt;br /&gt;        &lt;span style="color: blue"&gt;string &lt;/span&gt;imgUrl = urlHelper.Content(imgSrc);&lt;br /&gt;        TagBuilder imgTagBuilder = &lt;span style="color: blue"&gt;new &lt;/span&gt;TagBuilder(&lt;span style="color: #a31515"&gt;"img"&lt;/span&gt;);&lt;br /&gt;        imgTagBuilder.MergeAttribute(&lt;span style="color: #a31515"&gt;"src"&lt;/span&gt;, imgUrl);&lt;br /&gt;        &lt;span style="color: blue"&gt;string &lt;/span&gt;img = imgTagBuilder.ToString(TagRenderMode.Normal);&lt;br /&gt;        TagBuilder tagBuilder = &lt;span style="color: blue"&gt;new &lt;/span&gt;TagBuilder(&lt;span style="color: #a31515"&gt;"a"&lt;/span&gt;)&lt;br /&gt;        {&lt;br /&gt;            InnerHtml = img&lt;br /&gt;        };&lt;br /&gt;        tagBuilder.MergeAttribute(&lt;span style="color: #a31515"&gt;"href"&lt;/span&gt;, LinkBuilder.BuildUrlFromExpression(html.ViewContext.RequestContext, html.RouteCollection, action));&lt;br /&gt;        &lt;span style="color: blue"&gt;return &lt;/span&gt;tagBuilder.ToString(TagRenderMode.Normal);&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    &lt;span style="color: blue"&gt;public static string &lt;/span&gt;ActionLinkWithImage&amp;lt;TController&amp;gt;(&lt;span style="color: blue"&gt;this &lt;/span&gt;HtmlHelper html, Expression&amp;lt;Action&amp;lt;TController&amp;gt;&amp;gt; action, &lt;span style="color: blue"&gt;string &lt;/span&gt;imgSrc, &lt;span style="color: blue"&gt;object &lt;/span&gt;imageAttributes, &lt;span style="color: blue"&gt;object &lt;/span&gt;linkAttributes) where TController : Controller&lt;br /&gt;    {&lt;br /&gt;        UrlHelper urlHelper = &lt;span style="color: blue"&gt;new &lt;/span&gt;UrlHelper(html.ViewContext.RequestContext);&lt;br /&gt;        &lt;span style="color: blue"&gt;string &lt;/span&gt;imgUrl = urlHelper.Content(imgSrc);&lt;br /&gt;        TagBuilder imgTagBuilder = &lt;span style="color: blue"&gt;new &lt;/span&gt;TagBuilder(&lt;span style="color: #a31515"&gt;"img"&lt;/span&gt;);&lt;br /&gt;        imgTagBuilder.MergeAttribute(&lt;span style="color: #a31515"&gt;"src"&lt;/span&gt;, imgUrl);&lt;br /&gt;&lt;br /&gt;        imgTagBuilder.MergeAttributes(&lt;span style="color: blue"&gt;new &lt;/span&gt;RouteValueDictionary(imageAttributes));&lt;br /&gt;&lt;br /&gt;        &lt;span style="color: blue"&gt;string &lt;/span&gt;img = imgTagBuilder.ToString(TagRenderMode.Normal);&lt;br /&gt;&lt;br /&gt;        TagBuilder tagBuilder = &lt;span style="color: blue"&gt;new &lt;/span&gt;TagBuilder(&lt;span style="color: #a31515"&gt;"a"&lt;/span&gt;)&lt;br /&gt;        {&lt;br /&gt;            InnerHtml = img&lt;br /&gt;        };&lt;br /&gt;&lt;br /&gt;        tagBuilder.MergeAttributes(&lt;span style="color: blue"&gt;new &lt;/span&gt;RouteValueDictionary(linkAttributes));&lt;br /&gt;        tagBuilder.MergeAttribute(&lt;span style="color: #a31515"&gt;"href"&lt;/span&gt;, LinkBuilder.BuildUrlFromExpression(html.ViewContext.RequestContext, html.RouteCollection, action));&lt;br /&gt;        &lt;span style="color: blue"&gt;return &lt;/span&gt;tagBuilder.ToString(TagRenderMode.Normal);&lt;br /&gt;    }&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;An example of consumption on your page would then be:&lt;br /&gt;&lt;pre class="code"&gt;&lt;span style="background: yellow"&gt;&amp;lt;%&lt;/span&gt;&lt;span style="color: blue"&gt;= &lt;/span&gt;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) &lt;span style="background: yellow"&gt;%&amp;gt;&lt;br /&gt;&lt;/span&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&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;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/1171265999480681980-2615580093584870368?l=mirko.blogs.aspiant.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://mirko.blogs.aspiant.com/feeds/2615580093584870368/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://mirko.blogs.aspiant.com/2011/04/aspnet-mvc-strongly-typed-actionlink.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1171265999480681980/posts/default/2615580093584870368'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1171265999480681980/posts/default/2615580093584870368'/><link rel='alternate' type='text/html' href='http://mirko.blogs.aspiant.com/2011/04/aspnet-mvc-strongly-typed-actionlink.html' title='ASP.NET MVC Strongly-Typed ActionLink with Images'/><author><name>Mirko Geffken</name><uri>http://www.blogger.com/profile/08748094891809938994</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1171265999480681980.post-1138275870372326851</id><published>2009-06-13T08:58:00.000-07:00</published><updated>2011-04-01T09:01:05.462-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='ASP.NET MVC'/><category scheme='http://www.blogger.com/atom/ns#' term='C#'/><title type='text'>ASP.NET MVC Form Validation Error with Decimal Parsing</title><content type='html'>&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;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;p&gt;&lt;pre class="code"&gt;The model of type 'Foo' was not successfully updated.&lt;/pre&gt;&lt;/p&gt;&lt;p&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;/p&gt;&lt;pre class="code"&gt;Foo foo = _modelRepository.FindFoo(id);&lt;br /&gt;UpdateModel(foo);&lt;br /&gt;&lt;/pre&gt;The relatively simple fix is:&lt;pre class="code"&gt;Foo foo = _modelRepository.FindFoo(id);&lt;br /&gt;UpdateModel(foo, &lt;span style="color: #a31515"&gt;"SomeStringField"&lt;/span&gt;, &lt;span style="color: #a31515"&gt;"SomeOtherStringField"&lt;/span&gt;);&lt;br /&gt;&lt;br /&gt;&lt;span style="color: blue"&gt;if &lt;/span&gt;(!TryUpdateModel(foo, &lt;span style="color: #a31515"&gt;"TheDecimalField"&lt;/span&gt;))&lt;br /&gt;{&lt;br /&gt;    ModelState.AddModelError(&lt;span style="color: #a31515"&gt;"TheDecimalField"&lt;/span&gt;, &lt;span style="color: #a31515"&gt;"Does that look like a number to you?"&lt;/span&gt;);&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;/pre&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/1171265999480681980-1138275870372326851?l=mirko.blogs.aspiant.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://mirko.blogs.aspiant.com/feeds/1138275870372326851/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://mirko.blogs.aspiant.com/2009/06/aspnet-mvc-form-validation-error-with.html#comment-form' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/1171265999480681980/posts/default/1138275870372326851'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/1171265999480681980/posts/default/1138275870372326851'/><link rel='alternate' type='text/html' href='http://mirko.blogs.aspiant.com/2009/06/aspnet-mvc-form-validation-error-with.html' title='ASP.NET MVC Form Validation Error with Decimal Parsing'/><author><name>Mirko Geffken</name><uri>http://www.blogger.com/profile/08748094891809938994</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>1</thr:total></entry></feed>
