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):
Here is the View that is using my 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