Created
February 3, 2011 03:06
-
-
Save copenhas/808977 to your computer and use it in GitHub Desktop.
example of an html builder that htmlhelper extensions could return
This file contains hidden or 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
| public class PhoneHtmlBuilder<TModel> | |
| { | |
| private HtmlHelper<TModel> _helper; | |
| private Expression<Func<TModel, string>> _phoneNumber; | |
| private Expression<Func<TModel, string>> _extension; | |
| public PhoneHtmlBuilder(HtmlHelper<TModel> helper) | |
| { | |
| _helper = helper; | |
| } | |
| public PhoneHtmlBuilder<TModel> For(Expression<Func<TModel, string>> property) | |
| { | |
| _phoneNumber = property; | |
| return this; | |
| } | |
| public PhoneHtmlBuilder<TModel> WithExtension(Expression<Func<TModel, string>> property) | |
| { | |
| _extension = property; | |
| return this; | |
| } | |
| public MvcHtmlString Render() | |
| { | |
| //probably register javascript to get rendered or generate some | |
| //i'm not even sure if this compiles, just getting a basic format of the idea down | |
| return MvcHtmlString.Create( | |
| _helper.LabelFor(_property) + | |
| _helper.TextBoxFor(_property) + | |
| _withExtension ? _helper.TextBoxFor(_extension) : "" + | |
| _helper.ValidationMessageFor(_property)); | |
| } | |
| public override string ToString() | |
| { | |
| return Render().ToString(); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment