Problem creating a custom input element using FluentHtml (MVCContrib)
Posted
by seth
on Stack Overflow
See other posts from Stack Overflow
or by seth
Published on 2010-01-27T06:09:45Z
Indexed on
2010/05/08
7:18 UTC
Read the original article
Hit count: 255
Hi there, I just recently started dabbling in ASP.NET MVC 1.0 and came across the wonderful MVCContrib. I had originally gone down the path of creating some extended html helpers, but after finding FluentHTML decided to try my hand at creating a custom input element. Basically I am wanting to ultimately create several custom input elements to make it easier for some other devs on the project I'm working on to add their input fields to the page and have all of my preferred markup to render for them. So, in short, I'd like to wrap certain input elements with additional markup.. A TextBox would be wrapped in an <li />
for example.
I've created my custom input elements following Tim Scott's answer in another question on here: DRY in the MVC View.
So, to further elaborate, I've created my class, "TextBoxListItem":
public class TextBoxListItem : TextInput<TextBox>
{
public TextBoxListItem (string name) : base(HtmlInputType.Text, name) { }
public TextBoxListItem (string name, MemberExpression forMember, IEnumerable<IBehaviorMarker> behaviors) : base(HtmlInputType.Text, name, forMember, behaviors) { }
public override string ToString()
{
var liBuilder = new TagBuilder(HtmlTag.ListItem);
liBuilder.InnerHtml = ToString();
return liBuilder.ToString(TagRenderMode.SelfClosing);
}
}
I've also added it to my ViewModelContainerExtensions class:
public static TextBox TextBoxListItem<T>(this IViewModelContainer<T> view, Expression<Func<T, object>> expression) where T : class
{
return new TextBoxListItem(expression.GetNameFor(view), expression.GetMemberExpression(), view.Behaviors)
.Value(expression.GetValueFrom(view.ViewModel));
}
And lastly, I've added it to ViewDataContainerExtensions as well:
public static TextBox TextBoxListItem(this IViewDataContainer view, string name)
{
return new TextBox(name).Value(view.ViewData.Eval(name));
}
I'm calling it in my view like so:
<%= this.TextBoxListItem("username").Label("Username:") %>
Anyway, I'm not getting anything other than the standard FluentHTML TextBox, not wrapped in <li></li>
elements.
What am I missing here?
Thanks very much for any assistance.
© Stack Overflow or respective owner