Skip to main content

Extend MVC HtmlHelper methods...

I always like to use helper methods, and the ASP.NET MVC HtmlHelper methods are great. But sometimes you need some extra logic that they don't provide... Enter custom extension methods.

Here is a class I wrote specifically for extension methods (Note: Some of the extension methods are calling some static Utility methods, and their implementation is not shown here):

  01:  public static class HtmlHelperExtensions {

  02:   

  03:      public static string DateFormatterTextBox(this HtmlHelper htmlHelper, string name, 

  04:          DateTime date, string format, object attributes) {

  05:          string val = string.Format(format, date);

  06:          if (date.Year == 1900)

  07:              val = "0000-00-00";

  08:          return htmlHelper.TextBox(name, val, attributes);

  09:      }

  10:      public static string DateFormatterTextBox(this HtmlHelper htmlHelper, string name, 

  11:          DateTime date, string format) {

  12:          string val = string.Format(format, date);

  13:          if (date.Year == 1900)

  14:              val = "0000-00-00";

  15:          return htmlHelper.TextBox(name, val);

  16:      }

  17:      public static string TimeFormatterTextBox(this HtmlHelper htmlHelper, string name, 

  18:          int time, object attributes) {

  19:          string val = Utility.ConvertIntToTimeString(time);

  20:          return htmlHelper.TextBox(name, val, attributes);

  21:      }

  22:      public static string TimeFormatterTextBox(this HtmlHelper htmlHelper, string name, 

  23:          int time) {

  24:          string val = Utility.ConvertIntToTimeString(time);

  25:          return htmlHelper.TextBox(name, val);

  26:      }

  27:      public static string MaskFormatterTextBox(this HtmlHelper htmlHelper, string name, 

  28:          int mask, object attributes) {

  29:          string val = Utility.ConvertMaskToStartDays(mask);

  30:          return htmlHelper.TextBox(name, val, attributes);

  31:      }

  32:      public static string MaskFormatterTextBox(this HtmlHelper htmlHelper, string name, 

  33:          int mask) {

  34:          string val = Utility.ConvertMaskToStartDays(mask);

  35:          return htmlHelper.TextBox(name, val);

  36:      }

  37:  }




Here is the View that is using my extension methods...

  01:  <tr><td>Start Days:</td>

  02:      <td><%=Html.MaskFormatterTextBox("StartDays", ViewData.Model.StartDays.Value, 

  03:              new { size = 50 })%> 

  04:          <%=Html.ValidationMessage("StartDays", "*")%>

  05:      </td></tr>

  06:  <tr><td>Start Date:</td>

  07:      <td><%=Html.DateFormatterTextBox("StartDate", ViewData.Model.StartDate.Value,

  08:              "{0:yyyy-MM-dd}", new { size = 50 })%> 

  09:          <%=Html.ValidationMessage("StartDate", "*")%>

  10:      </td></tr>

  11:  <tr><td>End Date:</td>

  12:      <td><%=Html.DateFormatterTextBox("EndDate", ViewData.Model.EndDate.Value, 

  13:              "{0:yyyy-MM-dd}", new { size = 50 })%> 

  14:          <%=Html.ValidationMessage("EndDate", "*")%>

  15:      </td></tr>

  16:  <tr><td>Start Time:</td>

  17:      <td><%=Html.TimeFormatterTextBox("StartTime", ViewData.Model.StartTime.Value)%> 

  18:          <%=Html.ValidationMessage("StartTime", "*")%>

  19:      </td></tr>

  20:  <tr><td>End Time:</td>

  21:      <td><%=Html.TimeFormatterTextBox("EndTime", ViewData.Model.EndTime.Value)%> 

  22:          <%=Html.ValidationMessage("EndTime", "*")%>

  23:      </td></tr>


Comments

Popular posts from this blog

Welcome to the World!

Baby Tyler! Born 1/15/2008. Tyler Robert Morris

Tyler Eating

Here is a picture of Tyler eating Peas. They are all over his face and he loves it! If you look closely, you can see his first 2 teeth (bottom).

Configure Microsoft SSRS to use a Web Service as a Data Source...

I recently created a SQL Server Reporting Services (SSRS) report which used a Stored Procedure as a datasource, but the report needed some extra customization we're getting by using PaxScript in a similar web page. This isn't easily achievable using SQL stored procedures, so I decided to use a web service instead as the data source for the report. This way I have the advantage of LINQ/C# and PaxScript. Also, we have a need to run different versions of the web service, each for a different client. So the data source's web service URL needs to be dynamic, rather than static. Here's how it's done... In the report Data Source properties, the Type is XML and the Connection string value is a Report Parameter: Next, the report's Dataset Text should be in the following format: Note: There is a Parameter for each Web Service method parameter with the correct Name and Type. The ElementPath value is derived partly from the WSDL and partly f...