Search code examples
asp.net-mvcrazor

ASP.NET MVC razor selectlist with datavalue/datatext from child object


I have an IEnumerable<House> collection, and each House object contains a Room. How can I use the data from Room object for datatext/datavalue in SelectList construction? I can write some extra code to generate the proper object with datatext/datavalue. But I just wanted to know if there's another way using razor only.

@Model IEnumerable<House>
@Html.DropDownList("", new SelectList(Model, ???, ???)

Solution

  • Silly me. here's my solution:

    @Model IEnumerable<House> 
    @Html.DropDownList("dropdown", new SelectList(Model.select(x => x.Room, "Height", "Width")