T
T
TeVeTed2016-06-09 16:37:22
ASP.NET
TeVeTed, 2016-06-09 16:37:22

Where is the error in the controller?

I have the following controller:

public class CollegeController : Controller
    {
        private ICollegeRepository repository;
        public int pageSize = 10;
        public CollegeController(ICollegeRepository repo)
        {
            repository = repo;
        }

        public ViewResult List(string region,
                               string area,
                               string localityType, int page = 1)
        {
            CollegeListViewModel model = new CollegeListViewModel
            {
                Colleges = repository.Colleges
                .Where(p => (region == null || p.Area.Region.Name == region)
                && (localityType == null || p.LocalityType.Name == localityType))
                .Where(p => area == null || p.Area.Name == area)
                .OrderBy(college => college.CollegeId)
                .Skip((page - 1) * pageSize)
                .Take(pageSize),
                PagingInfo = new PagingInfo
                {
                    CurrentPage  = page,
                    ItemsPerPage = pageSize,
                    TotalItems   = ((region == null) && (localityType == null)) ?
                repository.Colleges.Count() :
                repository.Colleges.Where(college => college.Area.Region.Name == region)
                                   .Where(college => college.Area.Name == area)
                                   .Where(college => college.LocalityType.Name == localityType).Count()
                },
                CurrentLocalityType = localityType,
                CurrentRegion = region,
                CurrentArea = area,
            };
            return View(model);
        }
    }

The whole problem is that when you start, if you first pass region, and then localityType, everything is fine.
But if you try to pass localityType when null is passed to the region, localityType also passes null
In other words, you must pass region before passing localityType
How to fix this?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question