MVC 4寻呼搜索

时间:2014-04-21 12:05:55

标签: asp.net-mvc asp.net-mvc-4

我在我的项目中应用了搜索方法但是,我还有Action index() 变得更加恐怖。 (控制器类型'AdultLiteracyTeachersController'上的当前操作请求'Index'在以下操作方法之间不明确:)

public ViewResult Index(string searchstring, string currentFilter, int? page)
            {

                if (searchstring != null)
                {
                    page = 1;
                }
                else
                {
                    searchstring = currentFilter;
                }
                ViewBag.CurrentFilter = searchstring;
                var teachers = from r in db.AdulLiteracyTeachers select r;
                if (!string.IsNullOrEmpty(searchstring))
                {

                    teachers = teachers.Where(r => r.ALTName.ToUpper().Contains(searchstring.ToUpper()));

                }

                teachers = teachers.OrderBy(r => r.ALTName);
                int pagesize = 10;
                int pageNo = (page ?? 1);

                return View(teachers.ToPagedList(pageNo, pagesize));
            }

另一个ActionResult.index()

public ActionResult Index()
        {
            var adulliteracyteachers = db.AdulLiteracyTeachers.Include(a => a.District);
            return View(adulliteracyteachers.ToList());
        }

动作结果用于调用所有数据我怎样才能在单个索引中使用?

1 个答案:

答案 0 :(得分:0)

结合它们:

        public ViewResult Index(string searchstring, string currentFilter, int? page)
        {
            if (searchString == null && currentFilter == null && !page.HasValue)
            {
                // No parameters
                var adulliteracyteachers = db.AdulLiteracyTeachers.Include(a => a.District);
                return View(adulliteracyteachers.ToList());
            }

            // Regular code here down
            if (searchstring != null)
            {
                page = 1;
            }
            else
            {
                searchstring = currentFilter;
            }
            ViewBag.CurrentFilter = searchstring;
            var teachers = from r in db.AdulLiteracyTeachers select r;
            if (!string.IsNullOrEmpty(searchstring))
            {

                teachers = teachers.Where(r => r.ALTName.ToUpper().Contains(searchstring.ToUpper()));

            }

            teachers = teachers.OrderBy(r => r.ALTName);
            int pagesize = 10;
            int pageNo = (page ?? 1);

            return View(teachers.ToPagedList(pageNo, pagesize));
        }