public ActionResult Index() { IDatabaseUtility connection = new MySQLUtility(); try { connection.Connect(); CategoryListModel categorylist = new CategoryListModel(connection); List <Category> categories = categorylist.GetAll(); List <List <Post> > categoryposts = new List <List <Post> >(); foreach (Category category in categories) { PostListModel postlist = new PostListModel(connection); List <Post> posts = postlist.GetByCategoryLimit(category.link, 0, 5, "created_time DESC"); categoryposts.Add(posts); } ViewBag.categories = categories; return(View(categoryposts)); } catch (DBException e) { ViewBag.ErrorMessage = e.Message; return(View("_errors")); } }
public ActionResult ListByCategory(string category, int page = 1) { MySQLUtility connection = new MySQLUtility(); try { connection.Connect(); using (IDataReader result = connection.select("*").from("category").where ("link=" + new DBString(category).SqlValue()).Execute()){ if (result.Read()) { ViewBag.category = new Category(); ViewBag.category.id = (int)result["id"]; ViewBag.category.name = (string)result["name"]; ViewBag.category.link = (string)result["link"]; } else { ViewBag.ErrorMessage = "Danh mục này không tồn tại!"; return(View("_errors")); } } PostListModel list = new PostListModel(connection); List <Post> posts = list.GetByCategoryLimit(category, page, 20, "created_time desc"); PagePartitionModel pagepartition = new PagePartitionModel("ListByCategory", "PostShow", page, (int)Math.Ceiling(list.GetTotalPageByCategory(category) / 20.0)); ViewBag.pagepartition = pagepartition; return(View(posts)); } catch (DBException e) { ViewBag.ErrorMessage = e.Message; return(View("_errors")); } finally { connection.Close(); } }