Пример #1
0
        //public DataSet Select2()
        //{
        //    return dn.Select();
        //}

        public List <ViewNews> SelectSortList()
        {
            var     list = new List <ViewNews>();
            DataSet ds   = dn.SelectSortList();
            var     dt   = new DataTable();

            if (ds != null && ds.Tables.Count > 0)
            {
                dt = ds.Tables[0];
            }
            foreach (DataRow dr in dt.Rows)
            {
                var mod = new ViewNews();
                mod.ID           = Convert.ToInt32(dr["ID"]);
                mod.NewsSortName = Convert.ToString(dr["NewsSortName"]);
                mod.NewsTitle    = Convert.ToString(dr["NewsTitle"]);
                mod.CreatedTime  = Convert.ToDateTime(dr["CreatedTime"]);
                list.Add(mod);
            }
            return(list);
        }
Пример #2
0
        public ActionResult News(string commandName, int?page)
        {
            try
            {
                System.Linq.IQueryable <PersonalWebsite.Models.News> news;

                int    year = 0, month = 0;
                string type = null;
                if (Request["year"] != null && Request["year"] != "")
                {
                    year = Convert.ToInt32(Request["year"]);
                }
                if (Request["yearMonth"] != null && Request["yearMonth"] != "")
                {
                    year  = Convert.ToInt32(Request["yearMonth"].Split('-')[0]);
                    month = Convert.ToInt32(Request["yearMonth"].Split('-')[1]);
                }
                if (Request["type"] != null && Request["type"] != "")
                {
                    type = Request["type"];
                }

                //进行查询
                if (year != 0 && month == 0)
                {
                    //使用LINQ进行查询
                    news = from n in db.News
                           where n.NewsDate.Year == year
                           select n;
                }
                else if (year != 0 && month != 0)
                {
                    //使用LINQ进行查询
                    news = from n in db.News
                           where n.NewsDate.Year == year && n.NewsDate.Month == month
                           select n;
                }
                else if (type != null)
                {
                    //使用LINQ进行查询
                    news = from n in db.News
                           where n.NewsType == type
                           select n;
                }
                else
                {
                    news = from n in db.News
                           select n;
                }

                //进行分页
                news = news.OrderByDescending(n => n.NewsID);
                const int         pageItems   = 10;
                int               currentPage = (page ?? 1);
                IPagedList <News> pageNews    = news.ToPagedList(currentPage, pageItems);
                ViewNews          vnews       = new ViewNews();
                vnews.NewsList = pageNews;
                vnews.year     = year;
                vnews.month    = month;
                vnews.type     = type;

                return(View("News", vnews));
            }
            catch (Exception ex)
            {
                // return Content("<script>alert('Wrong arguments!');location.href='/Home/News';</script>");
                return(Content(ex.Message));
            }
        }