问题
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
回答1:
You can assign a default value to viewModel
:
private IEnumerable<SearchItems> GetItems(
ItemDescriptionFormViewModel viewModel = null)
{
if (viewModel == null)
viewModel = new ItemDescriptionFormViewModel();
This way, if you simply call GetItems()
, it will treat viewModel
as a new ItemDescriptionFormViewModel()
.
回答2:
If you want to keep both methods(I mean GetItems() and GetItems(viewModel)) they will automatically overload.
If you want the second method, you can pass parameter like this
private IEnumerable<SearchItems> GetItems(ItemDescriptionFormViewModel? viewModel)
Now you can pass null as its parameter.
回答3:
You're dereferencing a null reference here:
_itemDescriptionRepository.FindAll()
.OrderBy(c => c.Description)
.Where(a=>a.IsActive == true ||
a.ItemDescriptionId == viewModel.ItemDescriptionId) // if viewModel null, this throws
.Select(c => new SearchItems
{
Text = c.Description, Value = c.ItemDescriptionId.ToString()
});
So you could just update your Where()
clause to add pass if the view model is null:
...
.Where(a => a.IsActive || viewModel == null || viewModel.ItemDescriptionId == a.ItemDescriptionId)
Then you can pass null
as the argument safely, and you can even make a default parameter value so if no parameter is passed, it assumes the null
.
This is, of course, assuming ItemDescriptionFormViewModel
is a class, if it's a struct, then you'd have to make it nullable ItemDescriptionFormViewModel?
回答4:
declare it like this:
private IEnumerable<SearchItems> GetItems(ItemDescriptionFormViewModel viewModel = null)
and make sure to have a null
check in your method, something like this:
return
_itemDescriptionRepository.FindAll().OrderBy(c => c.Description).Where(a=> viewModel == null || (a.IsActive == true || a.ItemDescriptionId == viewModel.ItemDescriptionId)).Select(
c => new SearchItems {Text = c.Description, Value = c.ItemDescriptionId.ToString()});
this will allow you to call the method using GetItems()
and GetItems(ItemDescriptionFormViewModel viewModel)
EDIT: Disregard ? since ItemDescriptionFormViewModel is a class
回答5:
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
来源:https://stackoverflow.com/questions/10334582/passing-null-parameters