public ActionResult Create(Supplier supplier)
        {
            SupplierRepository repo = new SupplierRepository();
            if (ModelState.IsValid)
            {

                int countSupplier = repo.SupplierDuplicationCheck(supplier);
                //if exists. display an error.
                if (countSupplier > 0)
                {
                    ViewBag.DuplicateError = "Already Exists!";
                    return View(supplier);
                }

                int countAbbrevation = repo.SupplierAbbervationCheck(supplier);
                //if yes, display an error
                if (countAbbrevation > 0)
                {
                    ViewBag.AbbrevationDuplicateError = "Already Exists! Choose New.";
                    return View(supplier);
                }
                db.Suppliers.Add(supplier);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            return View(supplier);
        }
 public int SupplierDuplicationCheck(Supplier supplier)
 {
     //check if the input supplier name already exists
     List<Supplier> _supplier = (from s in db.Suppliers
                                 where s.Name == supplier.Name
                                 select s).ToList();
     return _supplier.Count;
 }
 public int SupplierAbbervationCheck(Supplier supplier)
 {
     //check if abbrevation already exists
     List<Supplier> _supplierAbbr = (from s in db.Suppliers
                                     where s.Abbrevation == supplier.Abbrevation
                                     select s).ToList();
     return _supplierAbbr.Count;
 }
        //Binding to Dropdownlist : Supplier
        public JsonResult BindDatatoDropdownSupplier()
        {
            var supplierList = (from s in db.Suppliers
                                select new { s.ID, s.Name }).ToList();

            List<Supplier> _supplier = new List<Supplier>();
            foreach (var item in supplierList)
            {
                Supplier s = new Supplier();
                s.ID = item.ID;
                s.Name = item.Name;
                _supplier.Add(s);
            }
            return Json(_supplier, JsonRequestBehavior.AllowGet);
        }
        public ActionResult Create(Supplier supplier)
        {
            SupplierRepository repo = new SupplierRepository();
            if (ModelState.IsValid)
            {                
                int countSupplier = repo.SupplierDuplicationCheck(supplier);

                if (Request.IsAjaxRequest())
                {
                    if (countSupplier > 0)
                    {
                        ViewBag.DuplicateError = "Already Exists!";
                        return Json("duplicate" , JsonRequestBehavior.AllowGet);
                    }

                    //Add supplier to dataSet
                    db.Suppliers.Add(supplier);
                    //save changes ToString database
                    db.SaveChanges();
                    //redirect to index 
                    return Json("success", JsonRequestBehavior.AllowGet);
                }


                //If already exists. display an error.
                if (countSupplier > 0)
                {
                    ViewBag.DuplicateError = "Already Exists!";
                    return View(supplier);
                }
                //Add supplier to dataSet
                db.Suppliers.Add(supplier);
                //save changes ToString database
                db.SaveChanges();
                //redirect to index        
                return RedirectToAction("Index");
            }

            return View(supplier);
        }
        public ActionResult Edit(Supplier supplier)
        {

            
            if (ModelState.IsValid &&  supplier != null)
            {

                var original = db.Suppliers.Find(supplier.ID);

                if(original.Name != supplier.Name)
                {
                    SupplierRepository repo = new SupplierRepository();
                    int countSupplier = repo.SupplierDuplicationCheck(supplier);
                    //If already exists. display an error.
                    if (countSupplier > 0)
                    {
                        ViewBag.DuplicateError = "Already Exists!";
                        return View(supplier);
                    }
                }
               

                db.Entry(original).CurrentValues.SetValues(supplier);
                //Save changes to Database
                db.SaveChanges();
                //redirect to Index page
                return RedirectToAction("Index");
            }

            return View(supplier);         
        }
        public ActionResult Edit(Supplier supplier)
        {
            SupplierRepository repo = new SupplierRepository();
            if (ModelState.IsValid)
            {
                string supplierNameCheck = supplier.Name.ToUpper();
                string abbrevationCheck = supplier.Abbrevation.ToUpper();

                supplier.Name = supplierNameCheck;
                supplier.Abbrevation = abbrevationCheck;
                db.Entry(supplier).State = EntityState.Modified;
                db.Suppliers.Add(supplier);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            return View(supplier);
        }