Esempio n. 1
0
        public static List <SelectListItem> PopulateItemCategorySelectListItem()
        {
            try
            {
                ItemCategoryLogic   itemCategoryLogic = new ItemCategoryLogic();
                List <ItemCategory> itemCategories    = itemCategoryLogic.GetAll();

                if (itemCategories == null && itemCategories.Count <= 0)
                {
                    return(new List <SelectListItem>());
                }

                List <SelectListItem> itemCategoryList = new List <SelectListItem>();
                if (itemCategories != null && itemCategories.Count > 0)
                {
                    SelectListItem list = new SelectListItem();
                    list.Value = "";
                    list.Text  = Select;
                    itemCategoryList.Add(list);

                    foreach (ItemCategory itemCategory in itemCategories)
                    {
                        SelectListItem selectList = new SelectListItem();
                        selectList.Value = itemCategory.Id.ToString();
                        selectList.Text  = itemCategory.ItemCategoryName;

                        itemCategoryList.Add(selectList);
                    }
                }

                return(itemCategoryList);
            }
            catch (Exception)
            {
                throw;
            }
        }
        public ActionResult UploadItem(SaleViewModel viewModel)
        {
            try
            {
                if (viewModel != null)
                {
                    if (Request.Files[0].ContentLength == 0)
                    {
                        SetMessage("Upload a Photo", Message.Category.Error);
                        RetainDropDownState(viewModel);
                        return(View());
                    }

                    HttpPostedFileBase hpf           = Request.Files[0] as HttpPostedFileBase;
                    string             pathForSaving = Server.MapPath("~/Content/Photos");
                    string             savedFileName = Path.Combine(pathForSaving, hpf.FileName);
                    //string[] getExtension = hpf.FileName.Split('.');
                    string extension    = Path.GetExtension(savedFileName); //"." + getExtension[1];
                    string invalidImage = InvalidFile(Request.Files[0].ContentLength, extension);
                    if (string.IsNullOrEmpty(invalidImage))
                    {
                        hpf.SaveAs(savedFileName);
                    }
                    else
                    {
                        SetMessage(invalidImage, Message.Category.Error);
                        RetainDropDownState(viewModel);
                        return(View());
                    }
                    using (TransactionScope scope = new TransactionScope())
                    {
                        ItemDetailLogic   itemDetailLogic   = new ItemDetailLogic();
                        ItemCategoryLogic itemCategoryLogic = new ItemCategoryLogic();
                        ItemTypeLogic     itemTypeLogic     = new ItemTypeLogic();
                        UserLogic         userLogic         = new UserLogic();
                        User user = userLogic.GetModelBy(u => u.User_Name == User.Identity.Name);
                        //ItemCategory itemCategory = new ItemCategory() { Id = 1 };
                        //ItemType itemType = new ItemType(){Id = 1};
                        //Location location = new Location(){Id = 1};
                        //string newItemDetailId = (itemDetailLogic.GetAll().LastOrDefault().Id + 1).ToString();

                        ItemDetail itemDetail = new ItemDetail();
                        itemDetail.ItemName        = viewModel.ItemDetail.ItemName;
                        itemDetail.ItemType        = viewModel.ItemDetail.ItemType;
                        itemDetail.ItemDescription = viewModel.ItemDetail.ItemDescription;
                        itemDetail.ItemCategory    = viewModel.ItemDetail.ItemCategory;
                        itemDetail.ItemPath        = "~/Content/Photos" + hpf.FileName;
                        itemDetail.NumberOfItem    = viewModel.ItemDetail.NumberOfItem;
                        itemDetail.Price           = viewModel.ItemDetail.Price;
                        itemDetail.Location        = viewModel.ItemDetail.Location;
                        itemDetail.TimeUploaded    = DateTime.Now;
                        itemDetail.AddedBy         = user;

                        ItemDetail newItemDetail = itemDetailLogic.Create(itemDetail);
                        scope.Complete();
                        TempData["Msg"] = "Operation Successful";
                        return(RedirectToAction("UploadItem"));
                    }
                }
            }
            catch (Exception ex)
            {
                SetMessage("Error! " + ex.Message, Message.Category.Error);
            }

            RetainDropDownState(viewModel);
            return(View(viewModel));
        }