Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

fubumvc - rendering a collection as a drop down list

Tags:

c#

fubumvc

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?

like image 443
stantona Avatar asked Jan 10 '12 21:01

stantona


1 Answers

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.

like image 190
Ian Battersby Avatar answered Oct 21 '22 17:10

Ian Battersby