I am currently having some trouble wrapping my head around this issue. I had a method that didn’t require any parameters and now i need to add a parameter but i don’t want to add the parameter in all the different places where the method is called. This is my current method :
private IEnumerable<SearchItems> GetItems(ItemDescriptionFormViewModel viewModel = null)
{
IOrderedQueryable<ItemDescription> items= _itemDescriptionRepository.FindAll().OrderBy(
c => c.Sort == null).ThenBy(
c => c.Sort).ThenBy(c => c.Description);
if(items.Count()==0)
ModelState.AddModelError("", string.Format("No active {0} entered.", Kids.Resources.Entities.ItemDescription.EntityNamePlural));
return
_itemDescriptionRepository.FindAll().OrderBy(c => c.Description).Where(a=>a.IsActive == true || viewModel == null || a.ItemDescriptionId == viewModel.ItemDescriptionId).Select(
c => new SearchItems {Text = c.Description, Value = c.ItemDescriptionId.ToString()});
}
I tried passing in null as a parameter for the other places where this method is being called but i am getting an error. What is the way to overload this issue?
PURPOSE: The purpose of adding the viewModel is because i have a dropdown list with active items to choose from. Once a user selects an active item and then for some reason that item becomes inactive and the user goes to edit their selection. In the dropdown list there should be the list of active items along with their previous selected item that is now inactive. I am using the ViewModel to check the id of the previously selected item.
Thanks
Everyone… Thanks for all the help and effort in trying to resolve this issue. I was finally able to work around it and here is who i fixed my problem: Checking List Item Id