showing inactive selected item in active item list

2019-08-06 17:00发布

问题:

I have a dropdown box which only shows active items in the list. But a user might have selected an item and saved and later that same item might be inactive. When the user edits their selection. The inactive item they have previously selected should be the default item in the list on the edit page along with other active items. I am not sure how to check for that. Here is my code which populates my list.

var meetingTypes = _meetingTypeRepository.FindAll().OrderBy(m => m.Description);
viewModel.MeetingTypes = meetingTypes.Where(a=> a.IsActive ?? false)
                                     .ToSelectList("MeetingTypeId", 
                                                   "Description",                
                                              viewModel.MeetingTypeId.ToString());

回答1:

I was able to figure it out just in case another person happened to have the same situation. Since i was using a viewModel which has all the item id's that i needed to compare against i used this in my Where clause and this is how my where clause changed to:

.Where(a=>a.IsActive == true || a.MeetingTypeId == viewModel.MeetingTypeId)

This allowed me to display the previously selected item by the user in the dropdown list even if that item is currently inactive.