fubumvc - rendering a collection as a drop down list
Asked Answered
C

1

5

I'm having trouble understanding how to render a collection as a drop down list.

If I have a model like:

public class AccountViewModel {             

    public string[] Country { get; set; }
}

I would like the string collection to render as a drop down list.

Using the html page helper InputFor doesn't seem to work. It simply render's a text box.

I've noticed that InputFor can reflect on the property type and render html accordingly. (Like a checkbox for a boolean field).

I also notice that FubuPageExtensions has methods for CheckBoxFor and TextBoxFor, but nothing equivalent to DropDownListFor.

I'm probably missing something quite fundamental in understanding html conventions in fubu.

Do I need to build the select tag myself? If so, what is the recommended approach to do it?

Chipmunk answered 10/1, 2012 at 21:51 Comment(0)
F
10

You are correct that (at the time I last looked) there is no FubuMVC.Core HTML extension method for generating select tags although you could use the HtmlTags library to generate a select tag via code.

As you touch upon in your question the correct way to attack this is likely with an HTML convention together with the HtmlTags library such as that demonstrated in the FubuMVC.Recipes example 'src/UI/HtmlConventionsWithPageExtensions'.

For example an enum generation example might be:

this.Editors
    .If(e => e.Accessor.PropertyType.IsEnum)
    .BuildBy(er =>
    {
        var tag = new HtmlTag("select");
        var enumValues = Enum.GetValues(er.Accessor.PropertyType);
        foreach (var enumValue in enumValues)
        {
            tag.Children.Add(new HtmlTag("option").Text(enumValue.ToString()));
        }

        return tag;
    });

The FubuMVC.Recipes repository is quite new and still growing so there may be some better examples around but hope this gives you some ideas.

Fishplate answered 10/1, 2012 at 22:26 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.