public ActionResult AddComment(FormCollection collection) { string content = collection["Content"]; content = HttpUtility.UrlDecode(content); int product = int.Parse(collection["Product"]); Product commentProduct = db.Products.Find(product); UserManager <ApplicationUser> UserManager = new UserManager <ApplicationUser>(new UserStore <ApplicationUser>(ApplicationDbContext.Create())); Comment comment; string username; if (!Request.IsAuthenticated) { comment = new Comment { Content = content, PublishDate = DateTime.Now, Likes = 0, Product = commentProduct }; username = "******"; } else { ApplicationUser user = UserManager.FindByName(User.Identity.Name); comment = new Comment { Content = content, PublishDate = DateTime.Now, Likes = 0, Product = commentProduct, UserID = user.Id }; username = user.UserName; } db.Comments.Add(comment); db.SaveChanges(); return(Json(new { success = true, content = content, date = comment.PublishDate.ToString("g"), username = username })); }
public ActionResult Create([Bind(Include = "Name")] Category category) { if (ModelState.IsValid) { db.Categories.Add(category); db.SaveChanges(); return(RedirectToAction("Index", "Admin")); } return(View(category)); }
private string getCartId(HttpContextBase context) { string userId = User.Identity.GetUserId(); if (userId == null) { userId = Guid.NewGuid().ToString(); } if (context.Session[CART_SESSION_KEY] == null) { context.Session[CART_SESSION_KEY] = userId; } else if (context.Session[CART_SESSION_KEY].ToString() != userId) { string session = context.Session[CART_SESSION_KEY].ToString(); var items = db.CartItems.Where(i => i.UserID == session).ToList(); foreach (var item in items) { item.UserID = userId; db.Entry(item).State = EntityState.Modified; } db.SaveChanges(); context.Session[CART_SESSION_KEY] = userId; } return(context.Session[CART_SESSION_KEY].ToString()); }
public ActionResult DirectedCreate(Manufacturer manufacturer) { try { if (ModelState.IsValid) { bool exists = db.Manufacturers.FirstOrDefault(i => i.Name == manufacturer.Name) != null; if (exists) { return(Json(new { success = false, message = "Đã có danh mục với tên này trong cơ sở dữ liệu!" })); } db.Manufacturers.Add(manufacturer); db.SaveChanges(); return(Json(new { success = true, name = manufacturer.Name, id = manufacturer.ID })); } } catch (DbUpdateException) { return(Json(new { success = false, message = "Dữ liệu nhập vào không hợp lệ!" })); } return(Json(new { success = false, message = "Dữ liệu nhập vào không hợp lệ!" })); }