Skip to content

Instantly share code, notes, and snippets.

@copenhas
Created February 3, 2011 03:06
Show Gist options
  • Select an option

  • Save copenhas/808977 to your computer and use it in GitHub Desktop.

Select an option

Save copenhas/808977 to your computer and use it in GitHub Desktop.
example of an html builder that htmlhelper extensions could return
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