Search code examples
asp.netmodel-view-controllerentity

ASP.NET MVC dropdown-detail view include a "ALL"


I am new in ASP.net MVC.

The website I am modifying has a drop-down list called "site", and users can change the site, then the rows of sales of the site would be shown accordingly.

Now my client wants the "site" drop-down list to include "All Sites", so when "All Sites" is selected, all sales across all sites would be shown.

Can someone give me a direction about what I should do? Should I change table mapping to let every sale always point to an actual site and dummy site called "All Sites"?

Thanks B


Solution

  • You can make a function with an optional parameter which will help us to decide whether we have to show sales of all sites or sales of only selected site.

    The function will looks something like this:

    public IEnumerable<Sale> GetSales(int? id)
        {
            if (id != null)
            {
                var salesBySite = getSalesBySite((int)id);
                return salesBySite;
            }
            else
            {
                var salesOfAllSites = getAllSales();
                return salesOfAllSites ;
            }
    
        }
    

    In drop-down list all options should have values as the ids of respective site but the option "AllSites" should not have value or empty value.

    When user select any site from dropdown, id will be passed to the function GetSales. If user select AllSites from dropdown then no id will be passed to function, function will return the sales of all sites.