控制器出错的地方?

时间:2016-06-09 13:56:52

标签: asp.net-mvc entity-framework-6

我们这里有一个控制器:

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);
    }
}

整个问题是当你开始传递第一个区域,然后传递localityType时,一切都很好。

但是如果你尝试传递localityType,那么在传递null的区域中,localityType也会发送null。

换句话说,在发送localityType之前必须传递区域

如何纠正?

对不起我的英文

0 个答案:

没有答案