public TEntity Update(TEntity entity) { var type = entity.GetType().GetProperty("Id"); object id = type.GetValue(entity, null); var local = dbSet.Find(id); context.Entry <TEntity>(local).State = EntityState.Detached; context.Entry <TEntity>(entity).State = EntityState.Modified; return(entity); }
protected void Session_Start() { var record = db.Statistics.Where(r => r.AccessDate.Day == DateTime.Now.Day && r.AccessDate.Month == DateTime.Now.Month && r.AccessDate.Year == DateTime.Now.Year).FirstOrDefault(); if (record == null) { db.Statistics.Add(new Model.Statistics { ProductID = 0, Accessions = 1, AccessDate = DateTime.Now }); } else { record.Accessions += 1; db.Entry(record).State = EntityState.Modified; } db.SaveChanges(); var themeSetting = db.AppSettings.Find("Theme"); if (themeSetting != null) { HttpCookie themeCookie = new HttpCookie("Theme", themeSetting.Value); Response.Cookies.Add(themeCookie); } }
public ActionResult Edit([Bind(Include = "ID,TypeName")] ProductType producttype) { if (ModelState.IsValid) { db.Entry(producttype).State = EntityState.Modified; db.SaveChanges(); return(RedirectToAction("Index")); } return(View(producttype)); }
public JsonResult ChargeSaleCode(int offerID) { try { var offer = db.Offers.Find(offerID); var userProperty = db.UserProperties.Find(User.Identity.Name); if (offer == null || userProperty == null) { return(Json(new { isSuccess = false, msg = "Tài khoản không tồn tại" })); } if (userProperty.Coins < offer.CoinsConsumed) { return(Json(new { isSuccess = false, msg = "Số xu không đủ" })); } var saleCode = new SaleCode() { Username = User.Identity.Name, Percent = offer.Percent, Code = Utility.CommonFunction.RandomString(10), OverduedDate = DateTime.Now }; db.SaleCodes.Add(saleCode); userProperty.Coins = userProperty.Coins - offer.CoinsConsumed; db.Entry(userProperty).State = EntityState.Modified; db.SaveChanges(); return(Json(new { isSuccess = true, msg = "", leftcoins = userProperty.Coins })); } catch (Exception ex) { return(Json(new { isSuccess = false, msg = ex.Message })); } }
public JsonResult SetTheme(string theme) { try { var themeSetting = db.AppSettings.Find("Theme"); themeSetting.Value = theme; db.Entry(themeSetting).State = EntityState.Modified; db.SaveChanges(); return(Json(new { IsSuccessful = 1 })); } catch (Exception ex) { return(Json(new { IsSuccessful = 0, Msg = ex.Message })); } }
public ActionResult Edit([Bind(Include = "ID,ProductName,Image,Url,Price,SalePrice,IsOnSale,ShortDesc,Desc,ProductTypeID,UploaderID")] Product product, HttpPostedFileBase imgFile) { if (ModelState.IsValid) { if (imgFile != null) { if (!string.IsNullOrEmpty(product.Image)) { string currentFilePath = Path.Combine(Server.MapPath("~/Content/UserImages/"), Path.GetFileName(product.Image)); if (System.IO.File.Exists(currentFilePath)) { System.IO.File.Delete(currentFilePath); } } string fileName = Path.GetFileNameWithoutExtension(imgFile.FileName); string extension = Path.GetExtension(imgFile.FileName); fileName = fileName + DateTime.Now.ToString("yymmssfff") + extension; product.Image = "~/Content/UserImages/" + fileName; fileName = Path.Combine(Server.MapPath("~/Content/UserImages/"), fileName); imgFile.SaveAs(fileName); } if (product.ID == 0) { db.Products.Add(product); } else { db.Entry(product).State = EntityState.Modified; } db.SaveChanges(); return(RedirectToAction("Index")); } ViewBag.ProductTypeID = new SelectList(db.ProductTypes, "ID", "TypeName", product.ProductTypeID); return(View(product)); }