Created
January 5, 2012 16:58
-
-
Save anonymous/1566126 to your computer and use it in GitHub Desktop.
NumberFor extension for HtmlHelper
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Linq.Expressions; | |
using System.Web.Mvc; | |
using System.Web.Mvc.Html; | |
using System.Text.RegularExpressions; | |
namespace Website.Extensions | |
{ | |
public static class HtmlExtensions | |
{ | |
public static MvcHtmlString NumberFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression) | |
{ | |
return htmlHelper.NumberFor(expression, null); | |
} | |
public static MvcHtmlString NumberFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression, object htmlAttributes) | |
{ | |
Regex regex = new Regex(@"type\s*=\s*""text""", RegexOptions.IgnoreCase); | |
string html = string.Empty; | |
if (htmlAttributes == null) | |
{ | |
html = InputExtensions.TextBoxFor(htmlHelper, expression).ToHtmlString(); | |
} | |
else | |
{ | |
html = InputExtensions.TextBoxFor(htmlHelper, expression, htmlAttributes).ToHtmlString(); | |
} | |
if(regex.IsMatch(html)) | |
{ | |
html = regex.Replace(html, "type=\"number\""); | |
} | |
return MvcHtmlString.Create(html); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment