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 i开发者_开发知识库s 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?
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
.
精彩评论