Reputation: 1496
dear all, I have a simple question that I cannot find an answer. I created a list view passing different data manipulation values in the url. my current url is something like: http://localhost/App/Product/List?categoryId=2&countryId=us.
I have products from different categories and countries, does anyone knows which value do I have to pass in order to display products from all countries or products from all categories instead of a particular one?. something like /List?categoryId=""&countryId=""
A colleague told me to perform the following: I think you should use nullable integer ( int? ) as a type for the categoryid parameter:
public ActionResult List(int? categoryid, int? page, string countryId)
{
var products = null;
if (categoryid.HasValue && !string.IsNullOrEmpty(countryId)) {
products = ProductRepository.FindCategoryProducts(categoryid, countryId).AsPagination(page.GetValueOrDefault(1), 20);
} else if (categoryid.HasValue == false && string.IsNullOrEmpty(countryId)) {
products = ProductRepository.GetAllProducts();
} else {
/// check the other cases here...
return View(products);
}
}
but it seems that if I have only two parameters could be possible but with many parameteres I will have lots of if - else if to display all possibilities.
is this the only way to filter data or any other option exists?
Any help will be appreciated. brgds
UPDATE!!:
Hi Jason, thank you so much. I have the following controller:
public ActionResult List(int categoryid, int? page, string countryId)
{
var products = ProductRepository.FindCategoryProducts(categoryid, countryId).AsPagination(page.GetValueOrDefault(1), 20);
return View(products);
}
Productrepository:
public IQueryable<Product> FindCategoryProducts(int categoryId, string countryId)
{
return from product in db.product
where product.CategoryId == categoryId
&& product.CountryId == countryId
orderby product.CreatedOn
select product;
}
then I can pass through the url Product/list?categoryId=2&countryId=us . but if I want to retrieve all products no matter which country like using a nullable string I receive errors. that was why this colleague recommended me to create - if /elseif statements - taking into consideration all parameters possibilities but it seems to be a long statement if as an example I have 8 parameters.
thank you!
Upvotes: 1
Views: 805
Reputation: 89127
There isn't a magic answer to this. To allow filtering on a large number of nullable criteria, somewhere there is going to be logic to determine how to handle the null parameters. I would suggest making this the responsibility of ProductRepository, not your page's Controller. This makes it easier to reuse the same logic in other controllers, instead of having to copy and paste it.
if you are using LINQ, you can do something like this in your Where clause
where Product == (productParm ?? Product)
if you are doing this in SQL, there is a similar operator you can use
where Product = COALESCE(productParm, Product)
In terms of the query you provided, assuming you have additional string properties A & B to filter on:
public IQueryable<Product> FindCategoryProducts(int categoryId, string countryId, string filterA, string filterB)
{
return from product in db.product
where product.CategoryId == categoryId
&& product.CountryId == (String.IsNullOrEmpty(countryId) ? product.CountryID : countryId)
&& product.PropertyA == (String.IsNullOrEmpty(filterA) ? product.CategoryA : filterA)
&& product.PropertyB == (String.IsNullOrEmpty(filterB) ? product.CategoryB : filterB)
// and so on...
orderby product.CreatedOn
select product;
}
More about the ?: operator
Upvotes: 2