<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://scottonwriting.net/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Scott On Writing.NET : ASP.NET MVC</title><link>http://scottonwriting.net/sowblog/archive/tags/ASP.NET+MVC/default.aspx</link><description>Tags: ASP.NET MVC</description><dc:language>en</dc:language><generator>CommunityServer 2007 (Build: 20423.869)</generator><item><title>Using Templates to Display Boolean Values as Yes/No Options</title><link>http://scottonwriting.net/sowblog/archive/2011/11/22/using-templates-to-display-boolean-values-as-yes-no-options.aspx</link><pubDate>Tue, 22 Nov 2011 01:11:08 GMT</pubDate><guid isPermaLink="false">2814ed8b-42a8-4dfe-b0b1-a7acb3e6d762:200696</guid><dc:creator>Scott Mitchell</dc:creator><slash:comments>1</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://scottonwriting.net/sowblog/rsscomments.aspx?PostID=200696</wfw:commentRss><comments>http://scottonwriting.net/sowblog/archive/2011/11/22/using-templates-to-display-boolean-values-as-yes-no-options.aspx#comments</comments><description>&lt;p&gt;One of ASP.NET MVC’s most useful features is its powerful templating system. Templates were introduced with ASP.NET MVC 2 as a way to have the view render either a display- or editing-related user interface for the entire model or for a property of the model. The &lt;strong&gt;Html.DisplayFor&lt;/strong&gt; and &lt;strong&gt;Html.EditorFor&lt;/strong&gt; methods provide a strongly-typed mechanism for rendering the appropriate template for a particular model property:&lt;/p&gt;  &lt;pre class="brush: csharp;"&gt;@Html.DisplayFor(model =&amp;gt; model.PropertyName)

@Html.EditorFor(model =&amp;gt; model.PropertyName)&lt;/pre&gt;

&lt;p&gt;For an in-depth overview of templates in ASP.NET MVC, see Brad Wilson’s multi-part article series, &lt;a href="http://bradwilson.typepad.com/blog/2009/10/aspnet-mvc-2-templates-part-1-introduction.html"&gt;ASP.NET MVC 2 Templates&lt;/a&gt;. (The content in Brad’s articles apply the same to ASP.NET MVC 3.)&lt;/p&gt;

&lt;h2&gt;&lt;/h2&gt;

&lt;h2&gt;How Html.DisplayFor and Html.EditorFor Determine Which Template to Use&lt;/h2&gt;

&lt;p&gt;ASP.NET MVC includes a number of built-in display and editor templates. For instance, when rendering a display or editing template for a Boolean value ASP.NET MVC will render either a disabled or enabled checkbox. When rendering a display template for an email address, ASP.NET MVC will render an actual &lt;strong&gt;mailto:&lt;/strong&gt; anchor tag so that the email address is clickable. The editor template for a password property renders an &lt;strong&gt;&amp;lt;input type=”password” /&amp;gt;&lt;/strong&gt; textbox so that the user’s input is masked. Additionally, you can create your own custom display and editor templates.&lt;/p&gt;

&lt;p&gt;The &lt;strong&gt;Html.DisplayFor&lt;/strong&gt; and &lt;strong&gt;Html.EditorFor&lt;/strong&gt; methods, when called, need to determine which template to render for the given property. To make this determination these methods examine the specified property’s &lt;em&gt;metadata&lt;/em&gt;, which includes information like the property’s data type (&lt;strong&gt;bool&lt;/strong&gt;, &lt;strong&gt;string&lt;/strong&gt;, &lt;strong&gt;int&lt;/strong&gt;, etc.) along with attributes decorating the model properties. For instance, in your model you can decorate a property with a &lt;strong&gt;DataType&lt;/strong&gt; attribute to inform the view and the templating system the type of data this property expresses. In the following model, the property Password is decorated as a password data type, while the Bio property is decorated as a multiline text input (which will render a multiline textbox for this property’s editor template).&lt;/p&gt;

&lt;pre class="brush: csharp;"&gt;public class MyModel
{
    ...

    [DataType(DataType.Password)]
    public string Password { get; set; }

    [DataType(DataType.MultilineText)]
    public string Bio { get; set; }

    ...
}&lt;/pre&gt;

&lt;p&gt;Specifically, the &lt;strong&gt;Html.DisplayFor&lt;/strong&gt; and &lt;strong&gt;Html.EditorFor&lt;/strong&gt; methods consult the following metadata in this order:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;The &lt;strong&gt;UIHint&lt;/strong&gt; attribute - specifies the name of the template to use.&lt;/li&gt;

  &lt;li&gt;The &lt;strong&gt;DataType&lt;/strong&gt; attribute&lt;/li&gt;

  &lt;li&gt;The data type&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Creating a custom template in ASP.NET MVC is a breeze. Simply add &lt;strong&gt;DisplayTemplates&lt;/strong&gt; and &lt;strong&gt;EditorTemplates&lt;/strong&gt; subfolders to the &lt;strong&gt;Views\Shared&lt;/strong&gt; folder. Then add a view file (e.g., &lt;strong&gt;.cshtml&lt;/strong&gt;) to the subfolder with the name of the template. You can override the default templates by naming the template with the same name as the data type to override – such as &lt;strong&gt;String.cshtml&lt;/strong&gt; or &lt;strong&gt;DateTime.cshtml&lt;/strong&gt; – or you can give it a unique name – such as &lt;strong&gt;YesNo.cshtml&lt;/strong&gt; – in which case you specify the template to use via the &lt;strong&gt;UIHint&lt;/strong&gt; attribute. The custom template is a view that defines the incoming model via the &lt;strong&gt;@model&lt;/strong&gt; directive and emits the desired output.&lt;/p&gt;

&lt;h2&gt;&lt;a href="http://nbaweblog.com/images/sowblog/Templates_2B648F0C.png"&gt;&lt;img style="background-image: none; border-bottom: 0px; border-left: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: right; border-top: 0px; border-right: 0px; padding-top: 0px" title="Templates" border="0" alt="Templates" align="right" src="http://nbaweblog.com/images/sowblog/Templates_thumb_78948897.png" width="218" height="132" /&gt;&lt;/a&gt;Building a Yes/No Template for Boolean Values&lt;/h2&gt;

&lt;p&gt;ASP.NET MVC’s default display template for Boolean values renders a disabled checkbox, but replacing it with the text “Yes” or “No” is quite simple to do when using templates. Start by adding a new template to the &lt;strong&gt;Views\Shared\DisplayTemplates&lt;/strong&gt; folder named &lt;strong&gt;YesNo.cshtml &lt;/strong&gt;with the following content:&lt;/p&gt;

&lt;pre class="brush: csharp;"&gt;@model bool

@if (Model)
{
    &amp;lt;text&amp;gt;Yes&amp;lt;/text&amp;gt;
}
else
{
    &amp;lt;text&amp;gt;No&amp;lt;/text&amp;gt;
}&lt;/pre&gt;

&lt;p&gt;The display template starts by defining the incoming model’s type (&lt;strong&gt;bool&lt;/strong&gt;) via the &lt;strong&gt;@model&lt;/strong&gt; directive. Next, it emits the literal string “Yes” if the incoming value (&lt;strong&gt;Model&lt;/strong&gt;) is true, “No” otherwise. That’s all there is to it!&lt;/p&gt;

&lt;p&gt;At this point we have the display template defined, but no view will use it unless we specifically instruct it to do so. (In other words, ASP.NET MVC will continue to use its default display template for Boolean values, which is the disabled checkbox.) To instruct a particular model property to use our display template rather than the default, use the &lt;strong&gt;UIHint&lt;/strong&gt; attribute like so:&lt;/p&gt;

&lt;pre class="brush: csharp;"&gt;public class MyModel
{
    ...

    [UIHint(&amp;quot;YesNo&amp;quot;)]
    public bool ReceiveNewsletter { get; set; }

    ...
}&lt;/pre&gt;

&lt;p&gt;With those two pieces of the puzzle in place – the template itself and the &lt;strong&gt;UIHint&lt;/strong&gt; attribute – a view will render the text “Yes” or “No” when using the following code:&lt;/p&gt;

&lt;pre class="brush: csharp;"&gt;@Html.DisplayFor(model =&amp;gt; model.ReceiveNewsletter)&lt;/pre&gt;

&lt;p&gt;What about editing? Here we need to create a new template file with the same name (&lt;strong&gt;YesNo.cshtml&lt;/strong&gt;) in the &lt;strong&gt;Views\Shared\EditorTemplates&lt;/strong&gt; folder and then add the following content:&lt;/p&gt;

&lt;pre class="brush: csharp;"&gt;@model bool

@Html.DropDownList(&amp;quot;&amp;quot;, new SelectListItem[] { new SelectListItem() { Text = &amp;quot;Yes&amp;quot;, Value = &amp;quot;true&amp;quot;, Selected = Model }, new SelectListItem() { Text = &amp;quot;No&amp;quot;, Value = &amp;quot;false&amp;quot;, Selected = !Model }})&lt;/pre&gt;

&lt;p&gt;As with the display template, we start by defining the incoming model’s type (&lt;strong&gt;bool&lt;/strong&gt;) via the &lt;strong&gt;@model&lt;/strong&gt; directive. Then we render a drop-down using the &lt;strong&gt;Html.DropDownList&lt;/strong&gt; method. This drop-down is composed of two select options – Yes and No with the values true and false, respectively. The first item is selected if the incoming value (&lt;strong&gt;Model&lt;/strong&gt;) is true, while the second option is selected if the incoming value is false.&lt;/p&gt;

&lt;p&gt;And we’re done! The screen shot below shows the output of a Boolean value when decorated with the &lt;strong&gt;[UIHint(&amp;quot;YesNo&amp;quot;)]&lt;/strong&gt; attribute and displayed using the &lt;strong&gt;Html.EditorFor&lt;/strong&gt; method.&lt;/p&gt;

&lt;p&gt;&lt;a href="http://nbaweblog.com/images/sowblog/Templates2_57A115F0.png"&gt;&lt;img style="background-image: none; border-bottom: 0px; border-left: 0px; padding-left: 0px; padding-right: 0px; display: block; float: none; margin-left: auto; border-top: 0px; margin-right: auto; border-right: 0px; padding-top: 0px" title="Templates2" border="0" alt="Templates2" src="http://nbaweblog.com/images/sowblog/Templates2_thumb_29479043.png" width="242" height="97" /&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Happy Programming!&lt;/p&gt;&lt;img src="http://scottonwriting.net/aggbug.aspx?PostID=200696" width="1" height="1"&gt;</description><category domain="http://scottonwriting.net/sowblog/archive/tags/ASP.NET+MVC/default.aspx">ASP.NET MVC</category></item><item><title>ASP.NET Training in San Diego on Saturday, September 24th</title><link>http://scottonwriting.net/sowblog/archive/2011/09/15/asp-net-training-in-san-diego-on-saturday-september-24th.aspx</link><pubDate>Thu, 15 Sep 2011 18:19:39 GMT</pubDate><guid isPermaLink="false">2814ed8b-42a8-4dfe-b0b1-a7acb3e6d762:196040</guid><dc:creator>Scott Mitchell</dc:creator><slash:comments>0</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://scottonwriting.net/sowblog/rsscomments.aspx?PostID=196040</wfw:commentRss><comments>http://scottonwriting.net/sowblog/archive/2011/09/15/asp-net-training-in-san-diego-on-saturday-september-24th.aspx#comments</comments><description>&lt;p&gt;On Saturday September 24th I’ll be doing two four-hour &lt;a href="http://www.asp.net/mvc"&gt;ASP.NET MVC&lt;/a&gt; 3 and Razor training events in San Diego:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;&lt;strong&gt;&lt;a href="http://fuzzylogicinc.net/InDepth/Outline.aspx?coid=278f5309-69f6-494f-844d-0f2ef6c4f0b1"&gt;Getting Started with ASP.NET MVC 3 and Razor&lt;/a&gt;&lt;/strong&gt; – (8 AM to Noon) - This four hour morning class introduces ASP.NET MVC 3 and the new Razor view syntax, explores how to build ASP.NET MVC applications, covers key differences between Web Forms and ASP.NET MVC, and highlights the benefits of ASP.NET MVC. It is intended for developers who are interested in getting started with or learning more about ASP.NET MVC 3 and what it has to offer, as well as for developers using ASP.NET MVC 2 who want to see what's new with ASP.NET MVC 3.&lt;/li&gt;    &lt;li&gt;&lt;strong&gt;&lt;a href="http://fuzzylogicinc.net/InDepth/Outline.aspx?coid=107ae94e-87f7-4ddc-b3f1-7f6af3ac2efe"&gt;Building Real-World Web Applications with ASP.NET MVC 3&lt;/a&gt;&lt;/strong&gt; – (1 to 5 PM) - This four hour afternoon class examines building real-world web applications using ASP.NET MVC 3. Learn how to craft lean controllers that comprise only a few lines of code. See how to use display and editor templates to decouple the user interface from your views. And practice creating rich and expressive view-specific models. Best practices on mapping domain models to view models, on input validation, and other topics will be explored. (This class is intended for students who attended the morning’s &lt;em&gt;Getting Started with ASP.NET MVC and Razor&lt;/em&gt; class or who are already familiar with ASP.NET MVC.)&lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;Both classes will be held at the University City Center office building, which is located on the 805 off the Governor St. exit (directions and map). And for both classes, students receive electronic access and printed copies of the training materials. Parking is free and coffee, snacks and lunch are provided.&lt;/p&gt;  &lt;p&gt;To sign up, or to see a detailed outline for each course, please visit &lt;a href="http://fuzzylogicinc.net/InDepth"&gt;http://fuzzylogicinc.net/InDepth&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;Hope to see you there!&lt;/p&gt;&lt;img src="http://scottonwriting.net/aggbug.aspx?PostID=196040" width="1" height="1"&gt;</description><category domain="http://scottonwriting.net/sowblog/archive/tags/ASP.NET+MVC/default.aspx">ASP.NET MVC</category></item><item><title>Full Day ASP.NET MVC 3 Training Event in Los Angeles on August 27th, 2011</title><link>http://scottonwriting.net/sowblog/archive/2011/08/18/full-day-asp-net-mvc-3-training-event-in-los-angeles-on-august-27th-2011.aspx</link><pubDate>Thu, 18 Aug 2011 04:28:13 GMT</pubDate><guid isPermaLink="false">2814ed8b-42a8-4dfe-b0b1-a7acb3e6d762:194551</guid><dc:creator>Scott Mitchell</dc:creator><slash:comments>0</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://scottonwriting.net/sowblog/rsscomments.aspx?PostID=194551</wfw:commentRss><comments>http://scottonwriting.net/sowblog/archive/2011/08/18/full-day-asp-net-mvc-3-training-event-in-los-angeles-on-august-27th-2011.aspx#comments</comments><description>&lt;p&gt;In conjunction with the Los Angeles .NET Developer's Group I will be presenting &lt;a href="http://goo.gl/LQBKp"&gt;a full day, hands-on lab/presentation/training event on ASP.NET MVC 3 Tips, Tricks, and Best Practices on Saturday, August 27th&lt;/a&gt;. The event is a 8 hour training with breakfast and lunch served. Attendees are asked to bring their own laptop and participate in a hands-on session. This event is geared for developers already familiar with ASP.NET MVC.&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;Event: &lt;/strong&gt;LADOTNET Master Series: ASP.NET MVC3 – Tips, Tricks, and Best Practices with Scott Mitchell    &lt;br /&gt;&lt;strong&gt;Date&lt;/strong&gt;:&lt;strong&gt; &lt;/strong&gt;Saturday, August 27th, 2011    &lt;br /&gt;&lt;strong&gt;Cost:&lt;/strong&gt; $50.00 – &lt;a href="http://goo.gl/LQBKp"&gt;signup&lt;/a&gt;    &lt;br /&gt;&lt;strong&gt;Location:     &lt;br /&gt;&lt;/strong&gt;Outlook Amusements    &lt;br /&gt;2900 W. Alameda Ave, Suite 400    &lt;br /&gt;Burbank, CA 91505    &lt;br /&gt;[&lt;a href="http://www.bing.com/maps/default.aspx?v=2&amp;amp;style=r&amp;amp;lvl=100&amp;amp;where1=2900%20W.%20Alameda%20Ave%2CBurbank%2CCA%2C91505"&gt;Map&lt;/a&gt;]    &lt;br /&gt;&lt;strong&gt;Abstract:&lt;/strong&gt;&amp;#160; &lt;/p&gt;  &lt;p&gt;When building an ASP.NET MVC application developers are faced with a lot of questions. What comprises the Models in MVC? What techniques are available to build simple, yet powerful views? How best to keep your controllers from ballooning in size? And what sort of busywork and tedium can be offloaded to tools?   &lt;br /&gt;&amp;#160; &lt;br /&gt;In this all-day, hands-on event, attendees start with a &amp;quot;baseline&amp;quot; ASP.NET MVC 3 application and together, we will add new features. Along the way we'll explore a variety of tips and best practices. We'll encounter pain points and discuss and implement various solutions. Topics will range from high-level, over-arching ones like options for building a domain model and persistence layer, to ones very specific to ASP.NET MVC, like creating and using custom display and editor templates in your views. And there will be plenty of hands-on experience with popular open source tools like AutoMapper.    &lt;br /&gt;&amp;#160; &lt;br /&gt;This talk is tailored for developers who are already familiar with core ASP.NET MVC concepts and are comfortable building ASP.NET MVC applications.&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;Signup at:&lt;/strong&gt; &lt;a href="http://mastersseries004.eventbrite.com/"&gt;http://mastersseries004.eventbrite.com/&lt;/a&gt;&lt;/p&gt;&lt;img src="http://scottonwriting.net/aggbug.aspx?PostID=194551" width="1" height="1"&gt;</description><category domain="http://scottonwriting.net/sowblog/archive/tags/ASP.NET+MVC/default.aspx">ASP.NET MVC</category></item><item><title>Full Day ASP.NET MVC 3 Training Event in Los Angeles on May 21st, 2011</title><link>http://scottonwriting.net/sowblog/archive/2011/05/12/full-day-asp-net-mvc-3-training-event-in-los-angeles-on-may-21st-2011.aspx</link><pubDate>Thu, 12 May 2011 16:57:09 GMT</pubDate><guid isPermaLink="false">2814ed8b-42a8-4dfe-b0b1-a7acb3e6d762:187195</guid><dc:creator>Scott Mitchell</dc:creator><slash:comments>3</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://scottonwriting.net/sowblog/rsscomments.aspx?PostID=187195</wfw:commentRss><comments>http://scottonwriting.net/sowblog/archive/2011/05/12/full-day-asp-net-mvc-3-training-event-in-los-angeles-on-may-21st-2011.aspx#comments</comments><description>&lt;p&gt;In conjunction with the Los Angeles .NET Developer's Group I will be presenting &lt;a href="http://www.eventbrite.com/event/1553162551?utm_source=eb_email&amp;amp;utm_medium=email&amp;amp;utm_campaign=new_eventv2&amp;amp;utm_term=eventurl_text"&gt;a full day, hands-on lab and training event on ASP.NET MVC 3 on Saturday, May 21st&lt;/a&gt;. The event is a 8 hour training with breakfast and lunch served. Attendees are asked to bring their own laptop and participate in a hands-on session. This event is geared for intermediate to experienced web developers new to ASP.NET MVC (or just new to ASP.NET MVC 3 and the Razor syntax). &lt;/p&gt;  &lt;p&gt;Over the course of the day we’ll explore MVC fundamentals, step through the process of building an ASP.NET MVC application, and implement common web application scenarios, such as: creating master/detail reports; working with forms; and building Create/Read/Update/Delete screens (CRUD). We’ll also explore interesting and powerful ASP.NET MVC tools and features, including scaffolding, partial views, layouts and layout sections, and more.&lt;/p&gt;  &lt;p&gt;&lt;font size="4"&gt;&lt;strong&gt;Event: &lt;/strong&gt;LADOTNET Master Series: ASP.NET MVC3 with Scott Mitchell      &lt;br /&gt;&lt;strong&gt;Date&lt;/strong&gt;:&lt;strong&gt; &lt;/strong&gt;Saturday, May 21st, 2011      &lt;br /&gt;&lt;strong&gt;Cost:&lt;/strong&gt; $50.00 – &lt;/font&gt;&lt;a href="http://www.eventbrite.com/event/1553162551?utm_source=eb_email&amp;amp;utm_medium=email&amp;amp;utm_campaign=new_eventv2&amp;amp;utm_term=eventurl_text"&gt;&lt;font size="4"&gt;signup&lt;/font&gt;&lt;/a&gt;    &lt;br /&gt;&lt;font size="4"&gt;&lt;strong&gt;Location:       &lt;br /&gt;&lt;/strong&gt;Outlook Amusements      &lt;br /&gt;2900 W. Alameda Ave, Suite 400      &lt;br /&gt;Burbank, CA 91505      &lt;br /&gt;[&lt;/font&gt;&lt;a href="http://www.bing.com/maps/default.aspx?v=2&amp;amp;style=r&amp;amp;lvl=100&amp;amp;where1=2900%20W.%20Alameda%20Ave%2CBurbank%2CCA%2C91505"&gt;&lt;font size="4"&gt;Map&lt;/font&gt;&lt;/a&gt;&lt;font size="4"&gt;]     &lt;br /&gt;&lt;strong&gt;Abstract:&lt;/strong&gt;&amp;#160; In this all-day, hands-on event attendees will build a complete, functional and interesting data-driven website over the course of the day using Microsoft's ASP.NET MVC 3 framework and the new Razor syntax.      &lt;br /&gt;&lt;strong&gt;Signup at:&lt;/strong&gt; &lt;/font&gt;&lt;a href="http://www.eventbrite.com/event/1553162551?utm_source=eb_email&amp;amp;utm_medium=email&amp;amp;utm_campaign=new_eventv2&amp;amp;utm_term=eventurl_text"&gt;&lt;font size="4"&gt;http://www.eventbrite.com/event/1553162551?utm_source=eb_email&amp;amp;utm_medium=email&amp;amp;utm_campaign=new_eventv2&amp;amp;utm_term=eventurl_text&lt;/font&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;Hope to see you there!&lt;/p&gt;&lt;img src="http://scottonwriting.net/aggbug.aspx?PostID=187195" width="1" height="1"&gt;</description><category domain="http://scottonwriting.net/sowblog/archive/tags/ASP.NET+MVC/default.aspx">ASP.NET MVC</category></item><item><title>Enhancing the Store Locator ASP.NET MVC Application to Include Directions</title><link>http://scottonwriting.net/sowblog/archive/2010/08/27/enhancing-the-store-locator-asp-net-mvc-application-to-include-directions.aspx</link><pubDate>Fri, 27 Aug 2010 18:44:36 GMT</pubDate><guid isPermaLink="false">2814ed8b-42a8-4dfe-b0b1-a7acb3e6d762:168670</guid><dc:creator>Scott Mitchell</dc:creator><slash:comments>2</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://scottonwriting.net/sowblog/rsscomments.aspx?PostID=168670</wfw:commentRss><comments>http://scottonwriting.net/sowblog/archive/2010/08/27/enhancing-the-store-locator-asp-net-mvc-application-to-include-directions.aspx#comments</comments><description>&lt;p&gt;&lt;a href="http://scottonwriting.net/images/sowblog/PopupWithDirections_402DA8E1.jpg"&gt;&lt;img style="border-bottom: 0px; border-left: 0px; margin: 0px 0px 5px 5px; display: inline; border-top: 0px; border-right: 0px" title="The info popup includes a Directions link." border="0" alt="The info popup includes a Directions link." align="right" src="http://scottonwriting.net/images/sowblog/PopupWithDirections_thumb_13A478FB.jpg" width="244" height="149" /&gt;&lt;/a&gt;Earlier this year I wrote an article on &lt;a href="http://www.4guysfromrolla.com/"&gt;4GuysFromRolla.com&lt;/a&gt; showing how to &lt;a href="http://www.4guysfromrolla.com/articles/051910-1.aspx"&gt;build an ASP.NET WebForms store locator application using Google Maps&lt;/a&gt;. Recently, I ported the store locator application to ASP.NET MVC and detailed building the application in &lt;a href="http://www.4guysfromrolla.com/articles/081810-1.aspx"&gt;Implementing the Store Locator Application Using ASP.NET MVC&lt;/a&gt;.&lt;/p&gt;  &lt;p&gt; Recently, a reader wrote in and asked what steps would be necessary to include a “Directions” link with each marker in the map so that, when clicked, the user would see the driving directions from the address they entered and the store of interest. I decided to update the ASP.NET MVC application to include this new feature request. Now, the results page shows a “Directions” link in both the grid of nearby stores and in the info window that pops up when you click a map marker. Clicking the “Directions” link opens a new browser window and loads Google Maps, showing the directions from the user-entered address to the selected store’s address.&lt;/p&gt;  &lt;p&gt;To show the driving directions I send the user to the following URL: &lt;strong&gt;http://maps.google.com/maps?f=d&amp;amp;source=s_d&amp;amp;saddr=&lt;em&gt;startingAddress&lt;/em&gt;&amp;amp;daddr=&lt;/strong&gt;&lt;em&gt;&lt;strong&gt;destinationAddress&lt;/strong&gt;.&lt;/em&gt;&lt;/p&gt;  &lt;p&gt;   &lt;p&gt;&lt;/p&gt;    &lt;p&gt;When the user is sent to the store locator page, the user-entered address (a/k/a, the starting address) is passed through the querystring via a field named &lt;strong&gt;Address&lt;/strong&gt;, so we already know the starting address. But how do we get our hands on the destination address? Recall that view is passed a model that is a collection of &lt;strong&gt;NearbyStoreLocation&lt;/strong&gt; objects; the &lt;strong&gt;NearbyStoreLocation&lt;/strong&gt; class has properties like &lt;strong&gt;Address&lt;/strong&gt; (the street address), &lt;strong&gt;City&lt;/strong&gt;, &lt;strong&gt;Region&lt;/strong&gt;, &lt;strong&gt;PostalCode&lt;/strong&gt;, and so forth. We can build up the address by concatenating these various address parts.&lt;/p&gt;    &lt;p&gt;Rather than requiring the view to build up the address, I added a new read-only property to the &lt;strong&gt;NearbyStoreLocation&lt;/strong&gt; class named &lt;strong&gt;FormattedAddress&lt;/strong&gt;, which returns an address Google Maps can parse by piecing together the address-related properties into a string.&lt;/p&gt;    &lt;pre class="brush: csharp;"&gt;public string FormattedAddress
{
    get
    {
        var addrPieces = new List&amp;lt;string&amp;gt;(5);
        if (!string.IsNullOrEmpty(this.Address))
            addrPieces.Add(this.Address);
        if (!string.IsNullOrEmpty(this.City))
            addrPieces.Add(this.City);
        if (!string.IsNullOrEmpty(this.Region))
            addrPieces.Add(this.Region);
        if (!string.IsNullOrEmpty(this.CountryCode))
            addrPieces.Add(this.CountryCode);
        if (!string.IsNullOrEmpty(this.PostalCode))
            addrPieces.Add(this.PostalCode);

        return string.Join(&amp;quot;, &amp;quot;, addrPieces.ToArray());
    }
}&lt;/pre&gt;

  &lt;p&gt;In the view, the link to the directions can be build like so:&lt;/p&gt;

  &lt;pre class="brush: xml;"&gt;&amp;lt;a target=&amp;quot;_blank&amp;quot; href=&amp;quot;http://maps.google.com/maps?f=d&amp;amp;source=s_d&amp;amp;saddr=&amp;lt;%=Server.UrlEncode(Request.QueryString[&amp;quot;Address&amp;quot;]) %&amp;gt;&amp;amp;daddr=&amp;lt;%=Server.UrlEncode(store.FormattedAddress) %&amp;gt;&amp;quot;&amp;gt;Directions&amp;lt;/a&amp;gt;&lt;/pre&gt;

  &lt;p&gt;And that’s it! Adding the Directions link to the info popup window is a tad more involved because the quotation marks must be escaped using &lt;strong&gt;\”&lt;/strong&gt;.&lt;/p&gt;

  &lt;p&gt;To get your hands on the complete, working code, go to: &lt;a title="http://www.4guysfromrolla.com/code/GoogleMapsMVC.zip" href="http://www.4guysfromrolla.com/code/GoogleMapsMVC.zip"&gt;http://www.4guysfromrolla.com/code/GoogleMapsMVC.zip&lt;/a&gt;&lt;/p&gt;

  &lt;p&gt;Happy Programming!&lt;/p&gt;&lt;/p&gt;&lt;img src="http://scottonwriting.net/aggbug.aspx?PostID=168670" width="1" height="1"&gt;</description><category domain="http://scottonwriting.net/sowblog/archive/tags/ASP.NET+Talk/default.aspx">ASP.NET Talk</category><category domain="http://scottonwriting.net/sowblog/archive/tags/ASP.NET+MVC/default.aspx">ASP.NET MVC</category></item><item><title>I’m Teaching Two Upcoming Training Classes in San Diego</title><link>http://scottonwriting.net/sowblog/archive/2010/08/19/i-m-teaching-two-upcoming-training-classes-in-san-diego.aspx</link><pubDate>Thu, 19 Aug 2010 17:45:32 GMT</pubDate><guid isPermaLink="false">2814ed8b-42a8-4dfe-b0b1-a7acb3e6d762:168128</guid><dc:creator>Scott Mitchell</dc:creator><slash:comments>0</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://scottonwriting.net/sowblog/rsscomments.aspx?PostID=168128</wfw:commentRss><comments>http://scottonwriting.net/sowblog/archive/2010/08/19/i-m-teaching-two-upcoming-training-classes-in-san-diego.aspx#comments</comments><description>&lt;p&gt;Once a year (or so) I teach a handful of one-day, four hour classes in San Diego on various ASP.NET topics. I’ve got &lt;a href="http://www.fuzzylogicinc.net/InDepth/Outline.aspx"&gt;two such classes&lt;/a&gt; lined up for Saturday, September 11:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;&lt;strong&gt;&lt;a href="http://fuzzylogicinc.net/InDepth/Outline.aspx?coid=25ccc9a6-bdef-4265-888e-7111cc5660ca"&gt;Get Started with ASP.NET MVC&lt;/a&gt;&lt;/strong&gt; – &lt;em&gt;8 AM to Noon       &lt;br /&gt;&lt;/em&gt;Traditionally, ASP.NET websites have been designed using the Web Forms model; &lt;a href="http://www.asp.net/mvc"&gt;ASP.NET MVC&lt;/a&gt; is an alternative model that uses the Model-View-Controller pattern. In a nutshell, ASP.NET MVC gives developers much finer control over the markup rendered by their web pages, a greater and clearer separation of concerns, better testability, and cleaner, more SEO-friendly URLs. This four hour class introduces ASP.NET MVC, explores how to build ASP.NET MVC applications, covers key differences between Web Forms and ASP.NET MVC, and highlights the benefits of ASP.NET MVC. During the class, we will build a real-world ASP.NET MVC application from start to finish.      &lt;br /&gt;&lt;/li&gt;    &lt;li&gt;&lt;strong&gt;&lt;a href="http://fuzzylogicinc.net/InDepth/Outline.aspx?coid=bfb9f765-b5ca-4886-a8e6-b4d80668e18f"&gt;Build Responsive Web Applications with Ajax, jQuery, and ASP.NET&lt;/a&gt;&lt;/strong&gt; – &lt;em&gt;1 to 5 PM       &lt;br /&gt;&lt;/em&gt;Ajax-enabled web applications use JavaScript and streamlined client/server communications to offer a highly interactive and responsive user experience. &lt;a href="http://jquery.com/"&gt;jQuery&lt;/a&gt; is a free, popular and powerful JavaScript library that ships with Visual Studio 2010 and greatly simplifies building Ajax-enabled web applications. This four hour class examines key Ajax concepts and techniques and walks through building a real-world Ajax-enabled web application using jQuery and ASP.NET.&lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;Each class is $249 per student, but there is an ongoing early bird special price of $199 (available until August 28th). Students receive electronic access and printed copies of the training materials. Coffee, snacks and lunch are provided. The classes are held at the Sorrento Mesa Center (&lt;a href="http://fuzzylogicinc.net/InDepth/Directions.aspx"&gt;directions&lt;/a&gt;), which is located in Sorrento Valley and has free parking.&lt;/p&gt;  &lt;p&gt;If you are interested in signing up or would like detailed course outlines, visit &lt;a href="http://www.fuzzylogicinc.net/InDepth"&gt;www.fuzzylogicinc.net/InDepth&lt;/a&gt;. If you have any questions about the class, material, or logistics, shoot me an email at &lt;a href="mailto:mitchell@4guysfromrolla.com"&gt;mitchell@4guysfromrolla.com&lt;/a&gt;.&lt;/p&gt;  &lt;p&gt;Hope to see you there!&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;Signup: &lt;/strong&gt;&lt;a href="http://www.fuzzylogicinc.net/InDepth"&gt;www.fuzzylogicinc.net/InDepth&lt;/a&gt; &lt;/p&gt;  &lt;p&gt;&lt;/p&gt;&lt;img src="http://scottonwriting.net/aggbug.aspx?PostID=168128" width="1" height="1"&gt;</description><category domain="http://scottonwriting.net/sowblog/archive/tags/ASP.NET+Talk/default.aspx">ASP.NET Talk</category><category domain="http://scottonwriting.net/sowblog/archive/tags/Miscellaneous/default.aspx">Miscellaneous</category><category domain="http://scottonwriting.net/sowblog/archive/tags/ASP.NET+MVC/default.aspx">ASP.NET MVC</category><category domain="http://scottonwriting.net/sowblog/archive/tags/jQuery/default.aspx">jQuery</category></item><item><title>Using the GeneratedImage Control in ASP.NET MVC</title><link>http://scottonwriting.net/sowblog/archive/2010/08/14/using-the-generatedimage-control-in-asp-net-mvc.aspx</link><pubDate>Fri, 13 Aug 2010 23:44:56 GMT</pubDate><guid isPermaLink="false">2814ed8b-42a8-4dfe-b0b1-a7acb3e6d762:167923</guid><dc:creator>Scott Mitchell</dc:creator><slash:comments>6</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://scottonwriting.net/sowblog/rsscomments.aspx?PostID=167923</wfw:commentRss><comments>http://scottonwriting.net/sowblog/archive/2010/08/14/using-the-generatedimage-control-in-asp-net-mvc.aspx#comments</comments><description>&lt;p&gt;One of the free, open-source controls on the &lt;a href="http://aspnet.codeplex.com/"&gt;ASP.NET team’s CodePlex page&lt;/a&gt; is the &lt;a href="http://aspnet.codeplex.com/releases/view/16449"&gt;GeneratedImage control&lt;/a&gt;. In a nutshell, the GeneratedImage control is a combination of an ASP.NET Web Control and a set of classes that facilitate programmatically creating, serving, caching, and transforming images. If you store images in the database that need to be served from a web page, if you need to create images on the fly, or if you need to resize, add watermarks, or perform some other image transform, then the GeneratedImage control can help. For more information on the GeneratedImage control and its capabilities, see my articles &lt;a href="http://www.4guysfromrolla.com/articles/042209-1.aspx"&gt;Dynamically Generating and Caching Images in ASP.NET with the GeneratedImage Control&lt;/a&gt; and &lt;a href="http://www.4guysfromrolla.com/articles/042909-1.aspx"&gt;Image Transforms with the ASP.NET Generated Image Control&lt;/a&gt;.&lt;/p&gt;  &lt;p&gt;&lt;a href="http://aspnet.4guysfromrolla.com/images/GoogleMaps10.gif"&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; margin-left: 0px; border-left-width: 0px; margin-right: 0px" title="GoogleMaps10" border="0" alt="GoogleMaps10" align="right" src="http://scottonwriting.net/images/sowblog/GoogleMaps10_3A14BC44.gif" width="310" height="480" /&gt;&lt;/a&gt;Previously, my experience with the GeneratedImage control was in WebForms applications. However, I recently ported my &lt;a href="http://www.4guysfromrolla.com/articles/051910-1.aspx"&gt;Store Locator ASP.NET application&lt;/a&gt; from WinForms to ASP.NET MVC 2 (a two-part article series on porting this application from WinForms to ASP.NET MVC is forthcoming). As the screen shot to the right shows, the store locator results page uses the GeneratedImage control to dynamically create markers for the store locator map, namely a navy circle displaying a white number - 1, 2, 3, and so - to illustrate where on the map a particular store resides.&lt;/p&gt;  &lt;p&gt;This image is dynamically generated from an HTTP Handler named &lt;strong&gt;NumberToImageHandler.ashx&lt;/strong&gt;. Specifically, the text to display is passed through the querystring and this HTTP Handler generated and returns the image content. For example, pointing your browser to &lt;strong&gt;NumberToImageHandler.ashx?number=1&lt;/strong&gt; would create and return the binary content for the 1 image, whereas &lt;strong&gt;NumberToImageHandler.ashx?number=2&lt;/strong&gt; would create and return the binary content for the 2 image, and so on. This HTTP Handler is a class that extends the &lt;strong&gt;Microsoft.Web.ImageHandler&lt;/strong&gt; class, which is part of the GeneratedImage control library.&lt;/p&gt;  &lt;p&gt;For my ASP.NET MVC store locator application I wanted to utilize this existing code, but use a more natural route for accessing the content. Having a &lt;strong&gt;.ashx&lt;/strong&gt; extension just seems so passé. What I wanted instead was to reference these images using routes like:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;Images/1 &lt;/li&gt;    &lt;li&gt;Images/2 &lt;/li&gt;    &lt;li&gt;... &lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;Here’s what I ended up doing:&lt;/p&gt;  &lt;ol&gt;   &lt;li&gt;Added a new route in the &lt;strong&gt;RegisterRoutes&lt;/strong&gt; method in &lt;strong&gt;Global.asax &lt;/strong&gt;for the URL pattern &lt;strong&gt;Images/{number}&lt;/strong&gt;.       &lt;pre class="brush: csharp;"&gt;routes.Add(
    &amp;quot;ImageRoute&amp;quot;,
    new Route(
        &amp;quot;Images/{number}&amp;quot;,
        new RouteValueDictionary(),
        new RouteValueDictionary {{ &amp;quot;number&amp;quot;, &amp;quot;\\d+&amp;quot; }},
        new NumberRouteHandler()
    )
);&lt;/pre&gt;
Note the constraint that only matches this route if the &lt;em&gt;number&lt;/em&gt; parameter is one or more digits. Also note the RouteHandler class that is responsible for handling this route, &lt;strong&gt;NumberRouteHandler&lt;/strong&gt;. &lt;/li&gt;

  &lt;li&gt;Created the &lt;strong&gt;NumberRouteHandler&lt;/strong&gt; class. This class’s sole responsibility is to return an instance of the HTTP Handler that will handle this request. The code for this class is simple, it just returns an instance of the &lt;strong&gt;NumberToImageHandler&lt;/strong&gt; class: 

    &lt;pre class="brush: csharp;"&gt;public class NumberRouteHandler : IRouteHandler
{
    public IHttpHandler GetHttpHandler(RequestContext requestContext)
    {
        return new NumberToImageHandler();
    }
}&lt;/pre&gt;
  &lt;/li&gt;

  &lt;li&gt;Created the &lt;strong&gt;NumberToImageHandler&lt;/strong&gt; class. This class contains virtually the same code as the &lt;strong&gt;NumberToImageHandler.ashx&lt;/strong&gt; file in the WebForms application. The only difference was instead of reading the number to display from the querystring I read it from the &lt;strong&gt;Request&lt;/strong&gt; object’s &lt;strong&gt;RouteData&lt;/strong&gt; collection. Here’s the line of code that determines which number to display in the image: 

    &lt;pre class="brush: csharp;"&gt;string numberText = HttpContext.Current.Request.RequestContext.RouteData.Values[&amp;quot;number&amp;quot;] as string;&lt;/pre&gt;
  &lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;With this code in place, anytime a request comes in for Images/1, say, the ASP.NET Routing system dispatches the request to the &lt;strong&gt;NumberRouteHandler&lt;/strong&gt; class, which returns an instance of the &lt;strong&gt;NumberToImageHandler&lt;/strong&gt; class, which reads in the number (1, in this case), dynamically creates the image, and returns it back to the requestor. Pretty slick, eh?&lt;/p&gt;

&lt;p&gt;For more information on ASP.NET Routing see &lt;a href="http://www.4guysfromrolla.com/articles/012710-1.aspx"&gt;URL Routing in ASP.NET 4&lt;/a&gt; and &lt;a href="http://msdn.microsoft.com/en-us/library/cc668201.aspx"&gt;ASP.NET Routing&lt;/a&gt; (the technical docs). You can download the ASP.NET MVC version of the store locator from &lt;a title="http://www.4guysfromrolla.com/code/GoogleMapsMVC.zip" href="http://www.4guysfromrolla.com/code/GoogleMapsMVC.zip"&gt;http://www.4guysfromrolla.com/code/GoogleMapsMVC.zip&lt;/a&gt;. As noted earlier, there is a two-part article series in the works that walks through porting the store locator from WebForms to ASP.NET MVC and will be published on &lt;a href="http://www.4guysfromrolla.com/"&gt;4GuysFromRolla.com&lt;/a&gt; later this month.&lt;/p&gt;

&lt;p&gt;&lt;/p&gt;&lt;img src="http://scottonwriting.net/aggbug.aspx?PostID=167923" width="1" height="1"&gt;</description><category domain="http://scottonwriting.net/sowblog/archive/tags/ASP.NET+Talk/default.aspx">ASP.NET Talk</category><category domain="http://scottonwriting.net/sowblog/archive/tags/ASP.NET+MVC/default.aspx">ASP.NET MVC</category></item><item><title>I'll be speaking at the SoCal Code Camp this Weekend</title><link>http://scottonwriting.net/sowblog/archive/2010/06/23/i-ll-be-speaking-at-the-socal-code-camp-this-weekend.aspx</link><pubDate>Tue, 22 Jun 2010 23:06:00 GMT</pubDate><guid isPermaLink="false">2814ed8b-42a8-4dfe-b0b1-a7acb3e6d762:165746</guid><dc:creator>Scott Mitchell</dc:creator><slash:comments>0</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://scottonwriting.net/sowblog/rsscomments.aspx?PostID=165746</wfw:commentRss><comments>http://scottonwriting.net/sowblog/archive/2010/06/23/i-ll-be-speaking-at-the-socal-code-camp-this-weekend.aspx#comments</comments><description>&lt;p&gt;The &lt;a href="http://www.socalcodecamp.com/" mce_href="http://www.socalcodecamp.com/"&gt;SoCal Code Camp&lt;/a&gt; is coming to San Diego, California this weekend, June 26th and 27th. This free event boasts nearly 100 talks given by dozens of speakers from around the area covering a wide swath of interesting technology-related topics. This year the Code Camp is hosted at the beautiful &lt;a href="http://www.socalcodecamp.com/Directions.aspx" mce_href="http://www.socalcodecamp.com/Directions.aspx"&gt;UC-San Diego campus&lt;/a&gt;.&lt;br&gt;&lt;/p&gt;&lt;p&gt;I will be presenting two talks:&lt;/p&gt;&lt;ul&gt;&lt;li&gt;&lt;a href="http://www.socalcodecamp.com/session.aspx?sid=c6d6738f-872d-447a-8daa-1385d24acfd1" mce_href="http://www.socalcodecamp.com/session.aspx?sid=c6d6738f-872d-447a-8daa-1385d24acfd1"&gt;&lt;b&gt;Take Control of Your Website's URLs with ASP.NET Routing&lt;/b&gt;&lt;/a&gt; -&amp;nbsp; Sunday, June 27th, 9:00 AM, Rm 101&lt;br&gt;&lt;i&gt;Did you know that ASP.NET has a powerful URL routing framework built into it? This talk explores the ASP.NET Routing system and how to use it to create SEO-friendly, human-readble URLs in both MVC and Web Form applications.&lt;/i&gt;&lt;br&gt;&lt;br&gt;&lt;/li&gt;&lt;li&gt;&lt;a href="http://www.socalcodecamp.com/socalcodecamp/session.aspx?sid=f182f320-65a1-43b0-af2e-097005896cf5" mce_href="http://www.socalcodecamp.com/socalcodecamp/session.aspx?sid=f182f320-65a1-43b0-af2e-097005896cf5"&gt;&lt;b&gt;Getting Started with ASP.NET MVC&lt;/b&gt;&lt;/a&gt; - Sunday, June 27th, 10:15 AM, Rm 101&lt;br&gt;&lt;i&gt;ASP.NET MVC brings the Model-View-Controller design pattern to ASP.NET. In this talk, Scott shows how to get started with ASP.NET MVC, explores how to create and use Models, Views, and Controllers, and walks through a creating a simple, end-to-end ASP.NET MVC application.&lt;/i&gt;&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;If you plan on attending the &lt;i&gt;Getting Started with ASP.NET MVC&lt;/i&gt; talk I encourage you to also attend &lt;i&gt;Take Control of Your Website's URLs with ASP.NET Routing&lt;/i&gt;, as routing is an important aspect of ASP.NET MVC.&lt;/p&gt;&lt;p&gt;For a full list of talks being presented this year, see the &lt;a href="http://www.socalcodecamp.com/schedule.aspx" mce_href="http://www.socalcodecamp.com/schedule.aspx"&gt;schedule&lt;/a&gt; posted on the &lt;a href="http://www.socalcodecamp.com/" mce_href="http://www.socalcodecamp.com/"&gt;SoCal Code Camp website&lt;/a&gt;.&lt;/p&gt;&lt;p&gt;Hope to see you there!&lt;br&gt;&lt;/p&gt;&lt;img src="http://scottonwriting.net/aggbug.aspx?PostID=165746" width="1" height="1"&gt;</description><category domain="http://scottonwriting.net/sowblog/archive/tags/ASP.NET+Talk/default.aspx">ASP.NET Talk</category><category domain="http://scottonwriting.net/sowblog/archive/tags/ASP.NET+MVC/default.aspx">ASP.NET MVC</category></item><item><title>Resolve a URL from a Partial View (ASP.NET MVC)</title><link>http://scottonwriting.net/sowblog/archive/2010/05/11/resolve-a-url-from-a-partial-view-asp-net-mvc.aspx</link><pubDate>Mon, 10 May 2010 20:05:00 GMT</pubDate><guid isPermaLink="false">2814ed8b-42a8-4dfe-b0b1-a7acb3e6d762:163379</guid><dc:creator>Scott Mitchell</dc:creator><slash:comments>0</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://scottonwriting.net/sowblog/rsscomments.aspx?PostID=163379</wfw:commentRss><comments>http://scottonwriting.net/sowblog/archive/2010/05/11/resolve-a-url-from-a-partial-view-asp-net-mvc.aspx#comments</comments><description>&lt;P&gt;Working on an ASP.NET MVC application and needed the ability to &lt;B&gt;resolve a URL&lt;/B&gt; from a &lt;B&gt;partial view&lt;/B&gt;. For example, I have an image I want to display, but I need to resolve the virtual path (say, &lt;FONT face="Courier New"&gt;~/Content/Images/New.png&lt;/FONT&gt;) into a relative path that the browser can use, such as &lt;FONT face="Courier New"&gt;../../Content/Images/New.png&lt;/FONT&gt; or &lt;FONT face="Courier New"&gt;/&lt;I&gt;MyAppName&lt;/I&gt;/Content/Images/New.png&lt;/FONT&gt;. &lt;/P&gt;
&lt;P&gt;A standard view derives from the &lt;FONT face="Courier New"&gt;System.Web.UI.Page&lt;/FONT&gt; class, meaning you have access to the &lt;FONT face="Courier New"&gt;ResolveUrl&lt;/FONT&gt; and &lt;FONT face="Courier New"&gt;ResolveClientUrl&lt;/FONT&gt; methods. Consequently, you can write markup/code like the following:&lt;/P&gt;
&lt;BLOCKQUOTE style="MARGIN-RIGHT: 0px" dir=ltr&gt;
&lt;P&gt;&lt;FONT face="Courier New"&gt;&amp;lt;img src='=&amp;lt;%=Page.ResolveClientUrl("~/Content/Images/New.png")%&amp;gt;' /&amp;gt;&lt;/FONT&gt;&lt;/P&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;The problem is that the above code does not work as expected in a &lt;B&gt;partial view&lt;/B&gt;. What's a little confusing is that while the above code compiles and the page, when visited through a browser, renders, the call to &lt;FONT face="Courier New"&gt;Page.ResolveClientUrl&lt;/FONT&gt; returns precisely what you pass in, &lt;FONT face="Courier New"&gt;~/Content/Images/New.png&lt;/FONT&gt;, in this instance. The browser doesn't know what to do with &lt;FONT face="Courier New"&gt;~&lt;/FONT&gt;, it presumes it's part of the URL, so it sends the request to the server for the image with the &lt;FONT face="Courier New"&gt;~&lt;/FONT&gt; in the URL, which results in a broken image.&lt;/P&gt;
&lt;P&gt;I did a bit of searching online and found this handy tip from &lt;A href="http://stephenwalther.com/blog/" mce_href="http://stephenwalther.com/blog/"&gt;Stephen Walther&lt;/A&gt; - &lt;A href="http://stephenwalther.com/blog/archive/2009/02/18/asp.net-mvc-tip-47-ndash-using-resolveurl-in-an-html.aspx" mce_href="http://stephenwalther.com/blog/archive/2009/02/18/asp.net-mvc-tip-47-ndash-using-resolveurl-in-an-html.aspx"&gt;Using ResolveUrl in an HTML Helper&lt;/A&gt;. In a nutshell, Stephen shows how to create an extension method for the &lt;FONT face="Courier New"&gt;HtmlHelper&lt;/FONT&gt; class that uses the &lt;FONT face="Courier New"&gt;UrlHelper&lt;/FONT&gt; class to resolve a URL. Specifically, Stephen shows how to add an &lt;FONT face="Courier New"&gt;Image&lt;/FONT&gt; extension method to &lt;FONT face="Courier New"&gt;HtmlHelper&lt;/FONT&gt;. I incorporated Stephen's code into my codebase and also created a more generic extension method, which I named ResolveUrl.&lt;/P&gt;&lt;PRE class="brush: c#"&gt;public static MvcHtmlString ResolveUrl(this HtmlHelper htmlHelper, string url)
{
    var urlHelper = new UrlHelper(htmlHelper.ViewContext.RequestContext);
    return MvcHtmlString.Create(urlHelper.Content(url));
}&lt;/PRE&gt;
&lt;P&gt;With this method in place you can resolve a URL in a partial view like so:&lt;/P&gt;
&lt;BLOCKQUOTE style="MARGIN-RIGHT: 0px" dir=ltr&gt;
&lt;P&gt;&lt;FONT face="Courier New"&gt;&amp;lt;img src='&amp;lt;%=Html.ResolveUrl("~/Content/Images/New.png")%&amp;gt;' /&amp;gt;&lt;/FONT&gt;&lt;/P&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Or you could use Stephen's &lt;FONT face="Courier New"&gt;Html.Image&lt;/FONT&gt; extension method (although my more generic &lt;FONT face="Courier New"&gt;Html.ResolveUrl&lt;/FONT&gt; method could be used in non-image related scenarios where you needed to get a relative URL from a virtual one in a partial view). Thanks for the helpful tip, Stephen!&lt;/P&gt;
&lt;P&gt;Happy Programming!&lt;/P&gt;&lt;img src="http://scottonwriting.net/aggbug.aspx?PostID=163379" width="1" height="1"&gt;</description><category domain="http://scottonwriting.net/sowblog/archive/tags/ASP.NET+MVC/default.aspx">ASP.NET MVC</category></item></channel></rss>