// POST api/Post //public Object Post(Employee emp) public Object Post(EmployeeData emp) { //Save //db.Employees.Add(emp); db.EmployeeDatas.Add(emp); db.SaveChanges(); return(Json(new { data = emp })); }
public ActionResult Create([Bind(Include = "Id,Name")] User user) { if (ModelState.IsValid) { db.Users.Add(user); db.SaveChanges(); return(RedirectToAction("Index")); } return(View(user)); }
public ActionResult AddProduct(Product product, HttpPostedFileBase file) { MVCTestEntities _entity = new MVCTestEntities(); if (ModelState.IsValid) { if (file != null) { var fileName = Path.GetFileName(file.FileName); file.SaveAs(Path.Combine(Server.MapPath("~/Assets/Images/"), fileName)); product.ImagePath = fileName; } _entity.Products.Add(product); _entity.SaveChanges(); return(RedirectToAction("Index")); } else { product.CategoryList = new SelectList(_entity.Categories.AsEnumerable(), "CategoryID", "Name"); return(View(product)); } }
public ActionResult AddtoCart(FormCollection collection) { if (Session["UserID"] != null) // check user is loged in { if (Convert.ToString(collection["Quantity"]) != "") { int qnt = Convert.ToInt32(collection["Quantity"]); int price = Convert.ToInt32(collection["Price"]); int productid = Convert.ToInt32(collection["ProductID"]); MVCTestEntities _entity = new MVCTestEntities(); Cart crt = new Cart(); crt.ProductID = productid; crt.UserID = Convert.ToInt32(Session["UserID"]); crt.ProductQnt = qnt; crt.TotalPrice = qnt * price; _entity.Carts.Add(crt); _entity.SaveChanges(); Cart ct = new Cart(); List <Cart> lst = _entity.Carts.Where(x => x.UserID == crt.UserID).ToList(); var lst1 = (from mc in _entity.Carts join pr in _entity.Products on mc.ProductID equals pr.ProductID join ur in _entity.Users on mc.UserID equals ur.UserID where mc.UserID == crt.UserID select new { ProductName = pr.Name, ProductImage = pr.ImagePath, ProductQnt = mc.ProductQnt, TotalPrice = mc.TotalPrice, CartID = mc.CartID }).ToList(); List <MyCart> mcrt = new List <MyCart>(); for (int j = 0; j < lst1.Count; j++) { MyCart mct = new MyCart(); mct.ProductName = lst1[j].ProductName; mct.ProductQnt = lst1[j].ProductQnt; mct.ProductImage = lst1[j].ProductImage; mct.TotalPrice = lst1[j].TotalPrice; mct.CartID = lst1[j].CartID; mcrt.Add(mct); } return(View("MyCart", mcrt)); } else { return(View()); } } else { return(RedirectToAction("Index", "Account")); // redirect when user is not login } }
public ActionResult AddCategory(Category category) { MVCTestEntities _entity = new MVCTestEntities(); _entity.Categories.Add(category); _entity.SaveChanges(); // return View(); return(RedirectToAction("Index")); }
public ActionResult EditCart(FormCollection collection) { MVCTestEntities _entity = new MVCTestEntities(); Cart ct = new Cart(); ct.CartID = Convert.ToInt32(collection["CartID"]); ct.ProductQnt = Convert.ToInt32(collection["ProductQnt"]); ct.TotalPrice = Convert.ToInt32(collection["ProductQnt"]) * Convert.ToInt32(collection["UnitPrice"]); // update cart _entity.Carts.Attach(ct); var et = _entity.Entry(ct); et.Property(e => e.ProductQnt).IsModified = true; et.Property(e => e.TotalPrice).IsModified = true; _entity.SaveChanges(); var lst1 = (from mc in _entity.Carts join pr in _entity.Products on mc.ProductID equals pr.ProductID join ur in _entity.Users on mc.UserID equals ur.UserID where mc.CartID == ct.CartID select new { ProductName = pr.Name, ProductImage = pr.ImagePath, ProductQnt = mc.ProductQnt, TotalPrice = mc.TotalPrice, CartID = mc.CartID }).ToList(); List <MyCart> mcrt = new List <MyCart>(); for (int j = 0; j < lst1.Count; j++) { MyCart mct = new MyCart(); mct.ProductName = lst1[j].ProductName; mct.ProductQnt = lst1[j].ProductQnt; mct.TotalPrice = lst1[j].TotalPrice; mct.CartID = lst1[j].CartID; mct.ProductImage = lst1[j].ProductImage; mcrt.Add(mct); } Session["Result"] = mcrt; ViewBag.Myorder = mcrt; return(View("MyCart", mcrt)); }
public ActionResult RegisterUser(User user) { if (ModelState.IsValid) { MVCTestEntities _entity = new MVCTestEntities(); _entity.Users.Add(user); _entity.SaveChanges(); return(View("Index")); } else { return(View()); } }
public ActionResult Checkout(FormCollection collection) { ViewBag.AddressA = Convert.ToString(collection["Address"]); List <MyCart> crt = (List <MyCart>)ViewBag.Myorder; MVCTestEntities _entity = new MVCTestEntities(); int userid = Convert.ToInt32(Session["UserID"]); _entity.Carts.Where(x => x.UserID == userid).ToList().ForEach(p => _entity.Carts.Remove(p)); _entity.SaveChanges(); ViewBag.Myorder = null; return(View("Thankyou")); }
//DeleteCart public ActionResult DeleteCart(int id) { MVCTestEntities _entity = new MVCTestEntities(); var crt = _entity.Carts.Where(x => x.CartID == id).First(); _entity.Carts.Remove(crt); _entity.SaveChanges(); int userid = Convert.ToInt32(Session["UserID"]); var lst1 = (from mc in _entity.Carts join pr in _entity.Products on mc.ProductID equals pr.ProductID join ur in _entity.Users on mc.UserID equals ur.UserID where mc.UserID == userid select new { ProductName = pr.Name, ProductImage = pr.ImagePath, ProductQnt = mc.ProductQnt, TotalPrice = mc.TotalPrice, CartID = mc.CartID }).ToList(); List <MyCart> mcrt = new List <MyCart>(); for (int j = 0; j < lst1.Count; j++) { MyCart mct = new MyCart(); mct.ProductName = lst1[j].ProductName; mct.ProductQnt = lst1[j].ProductQnt; mct.TotalPrice = lst1[j].TotalPrice; mct.CartID = lst1[j].CartID; mct.ProductImage = lst1[j].ProductImage; mcrt.Add(mct); } Session["Result"] = mcrt; ViewBag.Myorder = mcrt; return(View("MyCart", mcrt)); }
public ActionResult DeleteEmployee(int id) { bool status = false; //using (EmployeeDataEntities dc = new EmployeeDataEntities()) using (MVCTestEntities dc = new MVCTestEntities()) { //var v = dc.Employees.Where(a => a.EmployeeID == id).FirstOrDefault(); var v = dc.EmployeeDatas.Where(a => a.EmployeeID == id).FirstOrDefault(); if (v != null) { //dc.Employees.Remove(v); dc.EmployeeDatas.Remove(v); dc.SaveChanges(); status = true; } } return(new JsonResult { Data = new { status = status } }); }
// GET: UserRegister //接收form表单返回的数据并存储到数据库中 public ActionResult Index(FormCollection collection) { string str = ""; //if (Request["textname"].Equals(null) || Request["pwname"].Equals(null) || Request["textname"] == "" || Request["pwname"].Equals("")) //{ // str = ""; // } if (Request["textname"] == null) { str = ""; } else { //声明一个上下文 MVCTestEntities mVCTestEntities = new MVCTestEntities(); //创建一个实体 Mytest0329.users firstuser = new users(); //firstuser.username = Request["textname"]; //firstuser.password = Request["pwname"]; firstuser.username = collection["textname"]; firstuser.password = collection["pwname"]; //在上下文中的表的类中添加实体 mVCTestEntities.users.Add(firstuser); //告诉上下文保存更改,即执行SQL语句操作 mVCTestEntities.SaveChanges(); ViewData["status"] = "注册成功"; } return(View()); }
//public ActionResult Save(Employee emp) public ActionResult Save(EmployeeData emp) { bool status = false; if (ModelState.IsValid) { //using (EmployeeDataEntities dc = new EmployeeDataEntities()) using (MVCTestEntities dc = new MVCTestEntities()) { if (emp.EmployeeID > 0) { //Edit //var v = dc.Employees.Where(a => a.EmployeeID == emp.EmployeeID).FirstOrDefault(); var v = dc.EmployeeDatas.Where(a => a.EmployeeID == emp.EmployeeID).FirstOrDefault(); if (v != null) { v.FirstName = emp.FirstName; v.LastName = emp.LastName; v.EmailID = emp.EmailID; v.City = emp.City; v.Country = emp.Country; } } else { //Save //dc.Employees.Add(emp); dc.EmployeeDatas.Add(emp); } dc.SaveChanges(); status = true; } } return(new JsonResult { Data = new { status = status } }); }