Search code examples
c#asp.net-mvcscaffolding

Model Field from DropDownList with Scaffolding


In my Model, I want to be able to select a property's value through a DropDownList. Like this:

public int Foo { get; set; }

private IEnumerable<string> FooOptions = new IEnumerable<string> { "Foo", "Bar" };

So, in the rendered html, I'd have something such as:

<select name="Foo">
    <option value="0">Foo</option>
    <option value="1">Bar</option>
</select>

This would be quite easy to do in the Controller and View, but I actually am searching for a way for this to work with Visual Studio's default Controller / View Scaffolder.

Is it even possible? Or even better, is there a better way to achieve this?

TIA, André


Solution

  • Take a look at this link: http://www.asp.net/mvc/tutorials/javascript/working-with-the-dropdownlist-box-and-jquery/examining-how-aspnet-mvc-scaffolds-the-dropdownlist-helper

    You'll notice, if you are using MVC with EF against a Model utilizing a foreign key (convert into a POCO with an IEnumerable), Visual Studio will automatically create a dropdownlist.