views:

26

answers:

1

Hello,

When I need to use a lookup I usually include the ID property in the view model class so I can use it this way in the corresponding view

<%= Html.LabelFor( model => model.LookupTableID )%>
<br />
<%= Html.DropDownListFor(model => model.LookupTableID, Model.LookuptableList, new {}) %>

having Model.LookuptableList as a property in the Model itself like this:

public IEnumerable<SelectListItem> LookuptableList {
    get {
        return GetLookuptableList().Select(
            t => new SelectListItem { Text = t.Description, Value = t.LookupTableID.ToString() } );
    }
}

But I am not sure that this is a good way to handle this because of the function GetLookuptableList() inside the view model class.

is there a better/cleaner way to do this?

+1  A: 

You are including the entire look up table with every instance of the view model. A little overkill. I usually create a static utility class with the look up table that returns the SelectListItem.

Dustin Laine