How to code a C# Extension method to turn a Domain Model object into an Interface object?

Posted by Dr. Zim on Stack Overflow See other posts from Stack Overflow or by Dr. Zim
Published on 2010-04-06T21:48:03Z Indexed on 2010/04/06 21:53 UTC
Read the original article Hit count: 275

When you have a domain object that needs to display as an interface control, like a drop down list, ifwdev suggested creating an extension method to add a .ToSelectList().

The originating object is a List of objects that have properties identical to the .Text and .Value properties of the drop down list. Basically, it's a List of SelectList objects, just not of the same class name.

I imagine you could use reflection to turn the domain object into an interface object. Anyone have any suggestions for C# code that could do this? The SelectList is an MVC drop down list of SelectListItem.

The idea of course is to do something like this in the view:

<%= Html.DropDownList("City", 
         (IEnumerable<SelectListItem>) ViewData["Cities"].ToSelectList() )

© Stack Overflow or respective owner

Related posts about asp.net-mvc

Related posts about ddd