public JsonResult AddMainCategory(string categoryName) { int orderId; int catId = DataProvider.AddMainCategory(categoryName, out orderId); CategoryListItem model = new CategoryListItem() { Id = catId, Name = categoryName, OrderId = orderId }; if (Session["Categories"] != null) { List<CategoryListItem> categories = (List<CategoryListItem>)Session["Categories"]; categories.Add(model); Session["Categories"] = categories; } string html = this.RenderPartialToString("Partials/_MainCategoryRow", model); return Json(new { html = html }); }
public static List<Product> GetProductsByCriteria(ProductsCriteria criteria, out CategoryListItem nextCategory) { nextCategory = null; List<Product> products = new List<Product>(); using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings[CONNECTION_STRING].ConnectionString)) { using (SqlCommand cmd = new SqlCommand("GetProductsByCriteria", conn)) { cmd.CommandType = CommandType.StoredProcedure; if (criteria.CategoryId != null) { cmd.Parameters.Add(new SqlParameter("@MainCat", criteria.CategoryId.Value)); } cmd.Parameters.Add(new SqlParameter("@PageNumber", criteria.PageNumber)); cmd.Parameters.Add(new SqlParameter("@PageSize", criteria.PageSize)); if (criteria.ProductId != null && criteria.ProductId.Value > 0) { cmd.Parameters.Add(new SqlParameter("@ProductId", criteria.ProductId.Value)); } conn.Open(); using (IDataReader reader = cmd.ExecuteReader()) { while (reader.Read()) { Product p = new Product() { Id = reader.GetInt32(0), Name = reader.GetString(3), ImageName = reader.GetString(1), SmallImageName = reader.GetString(5), CategoryId = reader.GetInt32(6), SubCategoryId = reader.GetInt32(2), CategoryName = reader.GetString(7), SubCategoryName = reader.IsDBNull(4) ? string.Empty : reader.GetString(4), PreviousProductId = reader.IsDBNull(12) ? null : (int?)reader.GetInt32(12), NextProductId = reader.IsDBNull(13) ? null : (int?)reader.GetInt32(13), }; products.Add(p); } if (criteria.CategoryId != null && reader.NextResult()) { if (reader.Read()) { nextCategory = new CategoryListItem() { Id = reader.GetInt32(0), Name = reader.GetString(1) }; } } } conn.Close(); } } return products; }
public JsonResult AddSubCategory(int mainCat, string subCat) { string mainCatName = GetCategories().First(x => x.Id == mainCat).Name; int orderId; int subCatId = DataProvider.AddSubCategory(subCat, mainCatName, mainCat, out orderId); CategoryListItem model = new CategoryListItem() { Id = subCatId, Name = subCat, OrderId = orderId }; ViewData["MainCategory"] = mainCat; string html = this.RenderPartialToString("Partials/_SubCategoryRow", model); return Json(new { html = html }); }
public static List<CategoryListItem> GetCategories(bool getAll = false) { List<CategoryListItem> categories = new List<CategoryListItem>(); using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings[CONNECTION_STRING].ConnectionString)) { using (SqlCommand cmd = new SqlCommand("GetCategories", conn)) { if (getAll) { cmd.Parameters.Add(new SqlParameter("@All", true)); } cmd.CommandType = CommandType.StoredProcedure; conn.Open(); using (IDataReader reader = cmd.ExecuteReader()) { int currentId = -1; int lastId = -1; int i = -1; while (reader.Read()) { lastId = reader.GetInt32(0); if (lastId != currentId) { if (i >= 0) { categories[i].SubCategories = categories[i].SubCategories.OrderBy(x => x.SubCategories).ToList(); } i++; currentId = lastId; CategoryListItem parent = new CategoryListItem() { Id = currentId, Name = reader.GetString(1), OrderId = reader.GetInt32(2), SubCategories = new List<CategoryListItem>() }; categories.Add(parent); } if (!reader.IsDBNull(3) && !reader.IsDBNull(4) && !reader.IsDBNull(5)) { CategoryListItem child = new CategoryListItem() { Id = reader.GetInt32(3), Name = reader.GetString(4), OrderId = reader.GetInt32(5) }; categories[i].SubCategories.Add(child); } } } conn.Close(); } } return categories.OrderBy(x => x.OrderId).ToList(); }