views:

137

answers:

1

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.

+1  A: 

Everything works...

public class TextBoxListItem : TextInput<TextBoxListItem>
{
    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 = base.ToString();
        return liBuilder.ToString(TagRenderMode.Normal);
    }
}
public static class ViewDataContainerExtensions
{
    public static TextBoxListItem TextBoxListItem(this IViewDataContainer view, string name)
    {
        return new TextBoxListItem(name).Value(view.ViewData.Eval(name));
    }
}
public static class ViewModelContainerExtensions
{
    public static TextBoxListItem 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));
    }
}
Jao
The difference is calling base.ToString() instead of ToString()
Tim Scott