Ejemplo n.º 1
0
        // GET: Product/Details/5
        public ActionResult Details(int?id)
        {
            if (id == null)
            {
                return(Redirect("/"));
            }

            var product = ProductBus.Find(id);

            if (product == null)
            {
                return(Redirect("/"));
            }

            ViewBag.Images = ImageBus.List(id);

            return(View(ProductBus.Details(id)));
        }
Ejemplo n.º 2
0
        public JsonResult SaveData(string name, string description, int id, int brand, int category, int type, int price, int quantity, int warranty)
        {
            bool   status  = false;
            string message = string.Empty;

            var model = new Product();

            model.Name        = name;
            model.Id          = id;
            model.Description = description;
            model.Price       = price;
            model.Quantity    = quantity;
            model.Warranty    = warranty;
            model.BrandId     = brand;
            model.CateId      = category;
            model.TypeId      = type;
            if (HttpContext.Request.Files.Count > 0)
            {
                // TODO: Add insert logic here
                var hpf = HttpContext.Request.Files[0];
                if (hpf.ContentLength > 0)
                {
                    string fileName             = Guid.NewGuid().ToString();
                    string fullPathWithFileName = "/Data/Images/" + fileName + ".jpg";
                    hpf.SaveAs(Server.MapPath(fullPathWithFileName));
                    model.ImageUrl = fullPathWithFileName;
                }
            }

            //add new productgory if id = 0
            if (model.Id == 0)
            {
                model.DateCreated  = DateTime.Now;
                model.DateModified = DateTime.Now;
                model.Status       = true;
                model.Viewed       = 0;
                model.Sold         = 0;
                try
                {
                    ProductBus.Add(model);
                    for (int i = 1; i < HttpContext.Request.Files.Count; i++)
                    {
                        var proImage = new Image();
                        var pro      = ProductBus.Find(model.Id);
                        proImage.ProductId = pro.Id;
                        proImage.Status    = true;
                        var hrf = HttpContext.Request.Files[i];
                        if (hrf.ContentLength > 0)
                        {
                            string fileName             = Guid.NewGuid().ToString();
                            string fullPathWithFileName = "/Data/Images/" + fileName + ".jpg";
                            hrf.SaveAs(Server.MapPath(fullPathWithFileName));
                            proImage.ImageUrl = fullPathWithFileName;
                            ImageBus.Add(proImage);
                        }
                    }
                    status = true;
                }
                catch (Exception ex)
                {
                    status  = false;
                    message = ex.Message;
                }
            }
            else
            {
                //update existing DB
                //save db
                var entity = ProductBus.Find(model.Id);
                if (HttpContext.Request.Files.Count > 0)
                {
                    // TODO: Add insert logic here
                    var hpf = HttpContext.Request.Files[0];
                    if (hpf.ContentLength > 0)
                    {
                        string fileName             = Guid.NewGuid().ToString();
                        string fullPathWithFileName = "/Data/Images/" + fileName + ".jpg";
                        hpf.SaveAs(Server.MapPath(fullPathWithFileName));
                        entity.ImageUrl = fullPathWithFileName;
                    }
                }
                entity.DateModified = DateTime.Now;
                entity.Name         = model.Name;
                entity.BrandId      = model.BrandId;
                entity.CateId       = model.CateId;
                entity.TypeId       = model.TypeId;
                entity.Price        = model.Price;
                entity.Quantity     = model.Quantity;
                entity.Description  = model.Description;
                entity.Warranty     = model.Warranty;
                entity.DateModified = DateTime.Now;
                try
                {
                    ProductBus.Edit(entity);
                    status = true;
                }
                catch (Exception ex)
                {
                    status  = false;
                    message = ex.Message;
                }
            }

            return(Json(new
            {
                status = status,
                message = message
            }));
        }