//protected void btnAdd_Click(object sender, EventArgs e) //{ // if(!IsPostBack) // { // lblModalTitle.Text = "Add new category"; // ScriptManager.RegisterStartupScript(Page, Page.GetType(), "myModal", "$('#myModal').modal();", true); // upModal.Update(); // } //} protected void btnSave_Click(object sender, EventArgs e) { if(txtCateID.Text == "") { Category cate = new Category(); cate.CategoryName = txtCateName.Text; cate.Description = txtDescription.Text; if (txtPicture.HasFile == true) { cate.Picture = txtPicture.FileBytes; } Cate.Add(cate); } else { Category cate = new Category(); cate.CategoryID = int.Parse(txtCateID.Text); cate.CategoryName = txtCateName.Text; cate.Description = txtDescription.Text; if (txtPicture.HasFile == true) { cate.Picture = txtPicture.FileBytes; } Cate.Edit(cate); } ReloadCateGrid(); ScriptManager.RegisterStartupScript(this, GetType(), "CloseAlert", "CloseAlert();", true); }
// // GET: /Product/ public ActionResult Index(int? page=1, int? categoryID=0) { List<Category> lsCate = new List<Category>(); var all = new Category { CategoryID = 0, CategoryName = "All", Description = "Load all category" }; lsCate.Add(all); lsCate.AddRange(_cate.GetAll()); // _uow.Repository<Category>().GetAll().ToList()); var selectedValue = lsCate.Find(c => c.CategoryID == categoryID); ViewBag.CategoryID = new SelectList(lsCate, "CategoryID", "CategoryName", selectedValue); ViewBag.SelectedCategoryID = categoryID; var products = _prod.GetAll(); // _uow.Repository<Product>().GetAll(); pageSize = 3; int pageNumber = (page ?? 1); if (categoryID != 0) { var rsl = products.OrderBy(i => i.ProductID).Where(p => p.CategoryID == categoryID).ToPagedList(pageNumber, pageSize); return View(rsl); } else { try { var rsl = products.OrderBy(i => i.ProductID).ToPagedList(pageNumber, pageSize); iRetry = 0; return View(rsl); } catch(Exception ex) { if (iRetry < 3) { iRetry++; return Index(page, categoryID); } else { iRetry = 0; return View(); } } } }
public ActionResult Create(Category category) { if (ModelState.IsValid) { if(Request.Files.Count >0 ) { var file = Request.Files["txtpicture"]; if(file != null && file.ContentLength > 0 ) { var red = new BinaryReader(file.InputStream); var filed = red.ReadBytes(file.ContentLength); //var content = Convert.ToBase64String(filed); //var fileName = Path.GetFileName(file.FileName); //var path = Path.Combine(Server.MapPath("~"),fileName); //file.SaveAs(path); category.Picture = filed; } } _cate.Add(category); return RedirectToAction("Index"); } return View(category); }
public ActionResult Edit(Category category) { if (ModelState.IsValid) { _cate.Edit(category); return RedirectToAction("Index"); } return View(category); }