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

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).

Backup Databases in a loop...

Note: This applies to Microsoft SQL Server 2005/2008...     Ever get sick of having a different SQL Job for each database backup? I created this method for backing up all databases (filtered) in a cursor. Just create a job with this as the Step and give the Job a schedule... 01: DECLARE @dbName varchar (50) 02: DECLARE @BKPDate varchar (10) 03: DECLARE @sql nvarchar (1000) 04: SET @BKPDate = replace ( convert ( varchar (10), getdate (), 102), '.' , '' ) 05: DECLARE bkpCursor CURSOR FOR 06: 07: /* only backup non-system databases */ 08: select [Name] from sys.databases where [owner_sid] <> 0x01 09: 10: OPEN bkpCursor 11: FETCH NEXT FROM bkpCursor INTO @dbName 12: IF @@FETCH_STATUS = 0 13: BEGIN 14: WHILE ( @@FETCH_STATUS = 0) 15: BEGIN 16: set @sql = 17: ' BACKUP DATABASE ' + @dbName + 18: ' TO...

SQL Generation

Recently I wrote a console application that generates SQL code to populate a database with sample data. I wanted it to be as generic as possible, but it still relies on a Source database to get sample data from. The application is configurable in that it lets you specify which Database is the source database, as well which tables to get the data out of. I did not want this application to depend on any stored procedures; it needs to be self contained, which is why I'm hard-coding the table definition sql query. So I call the console app via a batch file. The batch file calls the exe file, outputting the text to a file: 01: @ECHO OFF 02: bin\debug\SqlGenerator.exe > "C:\run.sql" 03: ECHO done. Here is the main method in the console app: 01: class Program { 02: static void Main( string [] args) { 03: Console.WriteLine(Database.GenerateSQL()); 04: } 05: } Here is the relevant excerpt from the App.config file: 01: < a...