<?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:pingback="http://madskills.com/public/xml/rss/module/pingback/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:dc="http://purl.org/dc/elements/1.1/" version="2.0">
  <channel>
    <title>Jeroen's Blog - MVC</title>
    <link>http://www.jeroenlandheer.com/Blog/</link>
    <description>A Dutch software developer living in Chile</description>
    <language>en-us</language>
    <copyright>Jeroen Landheer</copyright>
    <lastBuildDate>Fri, 05 Jun 2009 21:03:17 GMT</lastBuildDate>
    <generator>newtelligence dasBlog 2.3.9074.18820</generator>
    <managingEditor>jeroen@landheer.com</managingEditor>
    <webMaster>jeroen@landheer.com</webMaster>
    <item>
      <trackback:ping>http://www.jeroenlandheer.com/Blog/Trackback.aspx?guid=8d33584c-33ef-4f39-8b37-e6fad60b8aa2</trackback:ping>
      <pingback:server>http://www.jeroenlandheer.com/Blog/pingback.aspx</pingback:server>
      <pingback:target>http://www.jeroenlandheer.com/Blog/PermaLink,guid,8d33584c-33ef-4f39-8b37-e6fad60b8aa2.aspx</pingback:target>
      <dc:creator>Jeroen Landheer</dc:creator>
      <wfw:comment>http://www.jeroenlandheer.com/Blog/CommentView,guid,8d33584c-33ef-4f39-8b37-e6fad60b8aa2.aspx</wfw:comment>
      <wfw:commentRss>http://www.jeroenlandheer.com/Blog/SyndicationService.asmx/GetEntryCommentsRss?guid=8d33584c-33ef-4f39-8b37-e6fad60b8aa2</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
One of the requirements that I have on an application that I’m working on is that
the user can select a date easily when a textbox has focus. I wanted this to do in
such a manner that programmatically this can be done with minimum effort, and that
a few extra things are taken into account:
</p>
        <ul>
          <li>
Nullable dates (i.e. end dates) should be allowed with an empty text box 
</li>
          <li>
Not-nullable dates should default to the date of today 
</li>
          <li>
Multiple date pickers should be possible within the same form</li>
        </ul>
        <p>
          <strong>Ingredients</strong>
        </p>
        <p>
For this to work out, I’ve used the date picker control that’s available at <a title="http://jqueryui.com/" href="http://jqueryui.com/">http://jqueryui.com/</a> Besides
that, I’m using ASP.Net MVC 1.0
</p>
        <p>
          <strong>Creating an extension method for System.Web.Mvc.ViewUserControl</strong>
        </p>
        <p>
For the view, I’m using an extension method that’s called “DateField”. This allows
me to use the date field without having to use things like “RenderPartial” and creating
custom models. 
</p>
        <p>
This class looks like this:
</p>
        <pre class="brush: csharp;">public static class ViewUserControlDateFieldExtension
{
  public static string DateField(this IViewDataContainer control, string fieldName)
  {
    return DateField(control, fieldName, control.ViewData.Eval(fieldName) as DateTime?, false);
  }

  public static string DateField(this IViewDataContainer control, string fieldName, bool allowNulls)
  {
    return DateField(control, fieldName, control.ViewData.Eval(fieldName) as DateTime?, allowNulls);
  }

  public static string DateField(this IViewDataContainer control, string fieldName, DateTime? value)
  {
    return DateField(control, fieldName, value, false);
  }

  public static string DateField(this IViewDataContainer control, string fieldName, DateTime? value, bool allowNulls)
  {
    StringBuilder sb = new StringBuilder();
    sb.AppendFormat("&lt;input type=\"text\" name=\"{0}\" value=\"{1}\" class=\"datepicker\" /&gt;", fieldName, GetFieldValue(control, value, allowNulls));
    return sb.ToString();
  }

  private static string GetFieldValue(IViewDataContainer control, DateTime? value, bool allowNulls)
  {
    if (allowNulls)
    {
      if (value.HasValue)
        return value.Value.ToString("dd-MM-yyyy");
      else
        return String.Empty;
    }
    else
    {
      if (value.HasValue)
      {
        if (value.Value == DateTime.MinValue)
          return DateTime.Now.ToString("dd-MM-yyyy");
        else
          return value.Value.ToString("dd-MM-yyyy");
      }
      else
        return DateTime.Now.ToString("dd-MM-yyyy");
    }
  }
}
</pre>
        <p>
          <strong>Next, adding some jQuery script</strong>
        </p>
        <p>
Adding the script to the master page is easy. Just a few &lt;script&gt; tags and that’s
it. 
</p>
        <pre class="brush: xml;">&lt;link href="/themes/redmond/ui.all.css" type="text/css" rel="Stylesheet" /&gt;
&lt;script src="/Scripts/jquery-1.3.2.js" type="text/javascript"&gt;&lt;/script&gt;
&lt;script src="/Scripts/ui/ui.core.js" type="text/javascript"&gt;&lt;/script&gt;
&lt;script src="/Scripts/ui/ui.datepicker.js" type="text/javascript"&gt;&lt;/script&gt;
&lt;script src="/Scripts/ui/i18n/ui.datepicker-nl.js" type="text/javascript"&gt;&lt;/script&gt;
&lt;script type="text/javascript" language="javascript"&gt;
  // Configure and add a date picker.
  $(function() {
  $(".datepicker").datepicker({
    changeMonth: true,
    changeYear: true
  });
  $(".datepicker").datepicker('option', { dateFormat: 'dd-mm-yy' });
  $.datepicker.setDefaults($.extend({ showMonthAfterYear: false }, $.datepicker.regional['nl']))
});
&lt;/script&gt;
</pre>
        <p>
          <strong>Finally, use the class</strong>
        </p>
        <p>
Using the class is as easy as using this little line of code for a date field:
</p>
        <pre class="brush: csharp;">&lt;% = this.DateField("EndDate", true) %&gt;
</pre>
        <p>
The result looks like this:
</p>
        <p>
          <a href="http://www.jeroenlandheer.com/Blog/content/binary/WindowsLiveWriter/AddingsomeUIelementswithjQuerytoanMVCapp_EE26/image_2.png">
            <img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="http://www.jeroenlandheer.com/Blog/content/binary/WindowsLiveWriter/AddingsomeUIelementswithjQuerytoanMVCapp_EE26/image_thumb.png" width="245" height="210" />
          </a>
        </p>
        <p>
The most nice part? It works in IE7, IE8, Opera, Chrome, FireFox and Safari. 
</p>
        <p>
          <strong>UPDATE: Why use IViewDataContainer</strong>
        </p>
        <p>
I had a question from someone who wanted to know why I choose to extend IViewDataContainer
in stead of WebControl/WebPage. The reason is simple. Both the System.Web.Mvc.ViewPage
en System.Web.Mvc.ViewControl implement IViewDataContainer. This makes the interface
the easiest target to implement. 
</p>
        <img width="0" height="0" src="http://www.jeroenlandheer.com/Blog/aggbug.ashx?id=8d33584c-33ef-4f39-8b37-e6fad60b8aa2" />
        <br />
        <hr />
This website is sposored by <a href="http://www.thewheel.nl">The Wheel Automatisering</a>.</body>
      <title>Adding some UI elements with jQuery to an MVC app</title>
      <guid isPermaLink="false">http://www.jeroenlandheer.com/Blog/PermaLink,guid,8d33584c-33ef-4f39-8b37-e6fad60b8aa2.aspx</guid>
      <link>http://www.jeroenlandheer.com/Blog/2009/06/05/AddingSomeUIElementsWithJQueryToAnMVCApp.aspx</link>
      <pubDate>Fri, 05 Jun 2009 21:03:17 GMT</pubDate>
      <description>&lt;p&gt;
One of the requirements that I have on an application that I’m working on is that
the user can select a date easily when a textbox has focus. I wanted this to do in
such a manner that programmatically this can be done with minimum effort, and that
a few extra things are taken into account:
&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
Nullable dates (i.e. end dates) should be allowed with an empty text box 
&lt;li&gt;
Not-nullable dates should default to the date of today 
&lt;li&gt;
Multiple date pickers should be possible within the same form&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;
&lt;strong&gt;Ingredients&lt;/strong&gt;
&lt;/p&gt;
&lt;p&gt;
For this to work out, I’ve used the date picker control that’s available at &lt;a title="http://jqueryui.com/" href="http://jqueryui.com/"&gt;http://jqueryui.com/&lt;/a&gt; Besides
that, I’m using ASP.Net MVC 1.0
&lt;/p&gt;
&lt;p&gt;
&lt;strong&gt;Creating an extension method for System.Web.Mvc.ViewUserControl&lt;/strong&gt;
&lt;/p&gt;
&lt;p&gt;
For the view, I’m using an extension method that’s called “DateField”. This allows
me to use the date field without having to use things like “RenderPartial” and creating
custom models. 
&lt;/p&gt;
&lt;p&gt;
This class looks like this:
&lt;/p&gt;
&lt;pre class="brush: csharp;"&gt;public static class ViewUserControlDateFieldExtension
{
  public static string DateField(this IViewDataContainer control, string fieldName)
  {
    return DateField(control, fieldName, control.ViewData.Eval(fieldName) as DateTime?, false);
  }

  public static string DateField(this IViewDataContainer control, string fieldName, bool allowNulls)
  {
    return DateField(control, fieldName, control.ViewData.Eval(fieldName) as DateTime?, allowNulls);
  }

  public static string DateField(this IViewDataContainer control, string fieldName, DateTime? value)
  {
    return DateField(control, fieldName, value, false);
  }

  public static string DateField(this IViewDataContainer control, string fieldName, DateTime? value, bool allowNulls)
  {
    StringBuilder sb = new StringBuilder();
    sb.AppendFormat("&amp;lt;input type=\"text\" name=\"{0}\" value=\"{1}\" class=\"datepicker\" /&amp;gt;", fieldName, GetFieldValue(control, value, allowNulls));
    return sb.ToString();
  }

  private static string GetFieldValue(IViewDataContainer control, DateTime? value, bool allowNulls)
  {
    if (allowNulls)
    {
      if (value.HasValue)
        return value.Value.ToString("dd-MM-yyyy");
      else
        return String.Empty;
    }
    else
    {
      if (value.HasValue)
      {
        if (value.Value == DateTime.MinValue)
          return DateTime.Now.ToString("dd-MM-yyyy");
        else
          return value.Value.ToString("dd-MM-yyyy");
      }
      else
        return DateTime.Now.ToString("dd-MM-yyyy");
    }
  }
}
&lt;/pre&gt;
&lt;p&gt;
&lt;strong&gt;Next, adding some jQuery script&lt;/strong&gt;
&lt;/p&gt;
&lt;p&gt;
Adding the script to the master page is easy. Just a few &amp;lt;script&amp;gt; tags and that’s
it. 
&lt;/p&gt;
&lt;pre class="brush: xml;"&gt;&amp;lt;link href="/themes/redmond/ui.all.css" type="text/css" rel="Stylesheet" /&amp;gt;
&amp;lt;script src="/Scripts/jquery-1.3.2.js" type="text/javascript"&amp;gt;&amp;lt;/script&amp;gt;
&amp;lt;script src="/Scripts/ui/ui.core.js" type="text/javascript"&amp;gt;&amp;lt;/script&amp;gt;
&amp;lt;script src="/Scripts/ui/ui.datepicker.js" type="text/javascript"&amp;gt;&amp;lt;/script&amp;gt;
&amp;lt;script src="/Scripts/ui/i18n/ui.datepicker-nl.js" type="text/javascript"&amp;gt;&amp;lt;/script&amp;gt;
&amp;lt;script type="text/javascript" language="javascript"&amp;gt;
  // Configure and add a date picker.
  $(function() {
  $(".datepicker").datepicker({
    changeMonth: true,
    changeYear: true
  });
  $(".datepicker").datepicker('option', { dateFormat: 'dd-mm-yy' });
  $.datepicker.setDefaults($.extend({ showMonthAfterYear: false }, $.datepicker.regional['nl']))
});
&amp;lt;/script&amp;gt;
&lt;/pre&gt;
&lt;p&gt;
&lt;strong&gt;Finally, use the class&lt;/strong&gt;
&lt;/p&gt;
&lt;p&gt;
Using the class is as easy as using this little line of code for a date field:
&lt;/p&gt;
&lt;pre class="brush: csharp;"&gt;&amp;lt;% = this.DateField("EndDate", true) %&amp;gt;
&lt;/pre&gt;
&lt;p&gt;
The result looks like this:
&lt;/p&gt;
&lt;p&gt;
&lt;a href="http://www.jeroenlandheer.com/Blog/content/binary/WindowsLiveWriter/AddingsomeUIelementswithjQuerytoanMVCapp_EE26/image_2.png"&gt;&lt;img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="http://www.jeroenlandheer.com/Blog/content/binary/WindowsLiveWriter/AddingsomeUIelementswithjQuerytoanMVCapp_EE26/image_thumb.png" width="245" height="210"&gt;&lt;/a&gt;
&lt;/p&gt;
&lt;p&gt;
The most nice part? It works in IE7, IE8, Opera, Chrome, FireFox and Safari. 
&lt;/p&gt;
&lt;p&gt;
&lt;strong&gt;UPDATE: Why use IViewDataContainer&lt;/strong&gt;
&lt;/p&gt;
&lt;p&gt;
I had a question from someone who wanted to know why I choose to extend IViewDataContainer
in stead of WebControl/WebPage. The reason is simple. Both the System.Web.Mvc.ViewPage
en System.Web.Mvc.ViewControl implement IViewDataContainer. This makes the interface
the easiest target to implement. 
&lt;/p&gt;
&lt;img width="0" height="0" src="http://www.jeroenlandheer.com/Blog/aggbug.ashx?id=8d33584c-33ef-4f39-8b37-e6fad60b8aa2" /&gt;
&lt;br /&gt;
&lt;hr /&gt;
This website is sposored by &lt;a href="http://www.thewheel.nl"&gt;The Wheel Automatisering&lt;/a&gt;.</description>
      <comments>http://www.jeroenlandheer.com/Blog/CommentView,guid,8d33584c-33ef-4f39-8b37-e6fad60b8aa2.aspx</comments>
      <category>MVC</category>
      <category>Programming</category>
    </item>
  </channel>
</rss>