public ActionResult UploadImage(HttpPostedFileBase file, int id)
        {
            if (file != null)
            {
                string pic  = id.ToString() + ".jpg";//System.IO.Path.GetFileName(file.FileName);
                string path = System.IO.Path.Combine(
                    Server.MapPath("~/images/products"), pic);
                var product = db.Products.FirstOrDefault(b => b.Id == id);
                product.Image = "/images/products/" + pic;
                db.Products.AddOrUpdate(product);
                db.SaveChanges();
                // file is uploaded
                file.SaveAs(path);

                // save the image path path to the database or you can send image
                // directly to database
                // in-case if you want to store byte[] ie. for DB
                //using (MemoryStream ms = new MemoryStream())
                //{
                //    file.InputStream.CopyTo(ms);
                //    byte[] array = ms.GetBuffer();
                //}
            }
            // after successfully uploading redirect the user
            return(RedirectToAction("Index", "Products"));
        }
Exemplo n.º 2
0
        public ActionResult Comment([Bind(Include = "Id,Text,Rating,DatePosted,Poster")] Comment comment, int id)
        {
            comment.Rating     = 2;
            comment.DatePosted = DateTime.Now;
            var currentUserId = HttpContext.User.Identity.GetUserId();

            comment.Poster = db.Users.FirstOrDefault(u => u.Id == currentUserId);

            if (ModelState.IsValid)
            {
                var blogPost = db.BlogPosts.FirstOrDefault(p => p.Id == id);
                blogPost.Comments.Add(comment);
                db.BlogPosts.AddOrUpdate(blogPost);
                db.SaveChanges();
                return(RedirectToAction("Details", "BlogPosts", id));
            }
            return(View());
        }
Exemplo n.º 3
0
        public ActionResult Create([Bind(Include = "Id,OrderedOn,FullAddress,AditionalInfo")] Order order, int id)
        {
            string currentUserId = HttpContext.User.Identity.GetUserId();

            order.Product   = db.Products.FirstOrDefault(p => p.Id == id);
            order.OrderedOn = DateTime.Now;
            order.Buyer     = db.Users.FirstOrDefault(u => u.Id == currentUserId);
            order.Seller    = db.Products.FirstOrDefault(p => p.Id == id).Seller;
            this.service.RaiseProductRating(id);
            this.service.SendMail(order.Seller.Id);

            if (ModelState.IsValid)
            {
                db.Orders.Add(order);
                db.Users.FirstOrDefault(u => u.Id == order.Seller.Id).OrdersReceived.Add(order);
                db.SaveChanges();
                return(RedirectToAction("Index", "Products"));
            }
            return(View(order));
        }