private void RetainDropDownState(SaleViewModel viewModel)
        {
            try
            {
                if (viewModel.ItemType != null)
                {
                    ItemTypeLogic itemTypeLogic = new ItemTypeLogic();
                    ViewBag.ItemType = new SelectList(itemTypeLogic.GetAll(), "Id", "ItemTypeName", viewModel.ItemType.Id);
                }
                else
                {
                    ViewBag.ItemType = new SelectList(new List <Value>(), Utility.ID, Utility.TEXT);
                }

                ViewBag.ItemCategory = viewModel.ItemCategorySelectListItem;
                ViewBag.Location     = viewModel.LocationSelectListItem;
            }
            catch (Exception)
            {
                throw;
            }
        }
Esempio n. 2
0
        public static List <SelectListItem> PopulateItemTypeSelectListItem()
        {
            try
            {
                ItemTypeLogic   itemTypeLogic = new ItemTypeLogic();
                List <ItemType> itemTypes     = itemTypeLogic.GetAll();

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

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

                    foreach (ItemType itemType in itemTypes)
                    {
                        SelectListItem selectList = new SelectListItem();
                        selectList.Value = itemType.Id.ToString();
                        selectList.Text  = itemType.ItemTypeName;

                        itemTypeList.Add(selectList);
                    }
                }

                return(itemTypeList);
            }
            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));
        }