public async Task <ActionResult> Edit([Bind(Include = "CardID,BayID,Active,IsAssigned,LastActive,ModifiedDate,Remarks,CreatedDate,CardKey")] AccessCard accessCard, byte[] RowVersion)
        {
            string[] fieldsToBind = new string[] { "CardID", "BayID", "Active", "IsAssigned", "LastActive", "ModifiedDate", "Remarks", "CreatedDate", "CardKey" };
            if (ModelState.IsValid)
            {
                var accessCardToUpdate = await db.AccessCards.FindAsync(accessCard.CardID);

                if (accessCardToUpdate == null)
                {
                    AccessCard accessCardDeleted = new AccessCard();
                    TryUpdateModel(accessCardDeleted, fieldsToBind);
                    ModelState.AddModelError(string.Empty, "Unable to save changes. The Access Card was deleted by another user.");
                    return(RedirectToAction("Index"));
                }
                if (TryUpdateModel(accessCardToUpdate, fieldsToBind))
                {
                    string UpdatedUser = string.Empty;
                    try
                    {
                        UpdatedUser = accessCardToUpdate.ModifiedBy;
                        accessCardToUpdate.ModifiedDate = DateTime.Now;
                        accessCardToUpdate.ModifiedBy   = User.Identity.Name;
                        db.Entry(accessCardToUpdate).Property(x => x.CreatedDate).IsModified = false;
                        db.Entry(accessCardToUpdate).Property(x => x.CreatedBy).IsModified   = false;
                        db.Entry <AccessCard>(accessCardToUpdate).State           = EntityState.Modified;
                        db.Entry(accessCardToUpdate).OriginalValues["RowVersion"] = RowVersion;
                        await db.SaveChangesAsync();

                        return(RedirectToAction("Index"));
                    }
                    catch (DbUpdateConcurrencyException ex)
                    {
                        var entry         = ex.Entries.Single();
                        var clientValues  = (AccessCard)entry.Entity;
                        var databaseEntry = entry.GetDatabaseValues();
                        if (databaseEntry == null)
                        {
                            TempData["ErrorMessage"] = "Unable to save changes. The record was deleted";
                        }
                        else
                        {
                            TempData["ErrorMessage"] = "Unable to save changes. The record was edited by " + UpdatedUser + ". Select Updated Record.";
                        }
                    }
                }
                return(RedirectToAction("Index"));

                //db.Entry(accessCard).State = EntityState.Modified;
                //db.SaveChanges();
                //return RedirectToAction("Index");
            }
            ViewBag.BayID = new SelectList(db.Bays, "BayID", "Remarks", accessCard.BayID);
            return(View(accessCard));
        }
Пример #2
0
        // [ValidateAntiForgeryToken]
        //public ActionResult Edit([Bind(Include = "ProductID,ProductName,WeighOut,Active,LastActive,Remarks")] Product product)
        public async Task <ActionResult> Edit(int?ProductID, byte[] RowVersion)
        {
            string[] fieldsToBind = new string[] { "ProductID", "ProductName", "WeighOut", "Active", "LastActive", "Remarks" };
            if (ProductID == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }
            if (ModelState.IsValid)
            {
                Product product = new Product();
                //customer.CustomerID.Equals(0)
                if (ProductID.Equals(0) && TryUpdateModel(product, fieldsToBind))
                {
                    product.CreatedBy    = User.Identity.Name;
                    product.ModifiedBy   = User.Identity.Name;
                    product.CreatedDate  = DateTime.Now;
                    product.ModifiedDate = DateTime.Now;
                    db.Products.Add(product);
                    await db.SaveChangesAsync();

                    return(RedirectToAction("Index"));
                }
                var productToUpdate = await db.Products.FindAsync(ProductID);

                if (productToUpdate == null)
                {
                    Product productDeleted = new Product();
                    TryUpdateModel(productDeleted, fieldsToBind);
                    ModelState.AddModelError(string.Empty, "Unable to save changes. The product was deleted by another user.");
                    return(View(productDeleted));
                }
                if (TryUpdateModel(productToUpdate, fieldsToBind))
                {
                    string UpdatedUser = string.Empty;
                    try
                    {
                        UpdatedUser = productToUpdate.ModifiedBy;
                        productToUpdate.ModifiedDate = DateTime.Now;
                        productToUpdate.ModifiedBy   = User.Identity.Name;
                        db.Entry(productToUpdate).Property(x => x.CreatedDate).IsModified = false;
                        db.Entry(productToUpdate).Property(x => x.CreatedBy).IsModified   = false;
                        db.Entry <Product>(productToUpdate).State = EntityState.Modified;
                        db.Entry(productToUpdate).OriginalValues["RowVersion"] = RowVersion;
                        await db.SaveChangesAsync();

                        return(RedirectToAction("Index"));
                    }
                    catch (DbUpdateConcurrencyException ex)
                    {
                        var entry         = ex.Entries.Single();
                        var clientValues  = (Product)entry.Entity;
                        var databaseEntry = entry.GetDatabaseValues();
                        if (databaseEntry == null)
                        {
                            TempData["ErrorMessage"] = "Unable to save changes. The record was deleted";
                        }
                        else
                        {
                            TempData["ErrorMessage"] = "Unable to save changes. The record was edited by " + UpdatedUser + ". Select Updated Record.";
                        }
                    }
                }
            }
            //    product.ModifiedDate = DateTime.Now;
            //product.ModifiedBy = User.Identity.Name;
            //db.Entry(product).State = EntityState.Modified;
            //db.Entry(product).Property(x => x.CreatedDate).IsModified = false;
            //db.Entry(product).Property(x => x.CreatedBy).IsModified = false;
            //db.SaveChanges();
            else
            {
                var errors = ModelState.Values.SelectMany(v => v.Errors);
            }
            //return View(product);
            return(RedirectToAction("Index"));
        }
Пример #3
0
        // [ValidateAntiForgeryToken]
        //public ActionResult Edit([Bind(Include = "BayID,Frequency,CurrQueue,Active,LastActive,Remarks,ProductID")] Bay bay)
        public async Task <ActionResult> Edit(int?BayID, byte[] RowVersion, string[] ddlProduct)
        {
            if (ddlProduct == null)
            {
                TempData["ErrorMessage"] = "Please select the Product first.";
                return(RedirectToAction("Index"));
            }
            string[] fieldsToBind = new string[] { "BayID", "Frequency", "CurrQueue", "Active", "LastActive", "Remarks", "ProductID" };
            if (BayID == null)
            {
                return(new HttpStatusCodeResult(HttpStatusCode.BadRequest));
            }
            if (ModelState.IsValid)
            {
                var bayToUpdate = await db.Bays.FindAsync(BayID);

                var productID = Request["Frequency"].ToString();
                if (productID.Equals("-1"))
                {
                    Bay bayProduct = new Bay();
                    TryUpdateModel(bayProduct, fieldsToBind);

                    bayProduct.Frequency    = 0;
                    bayProduct.CurrQueue    = 0;
                    bayProduct.CreatedDate  = DateTime.Now;
                    bayProduct.ModifiedDate = DateTime.Now;
                    bayProduct.CreatedBy    = User.Identity.Name;
                    bayProduct.ModifiedBy   = User.Identity.Name;
                    db.Bays.Add(bayProduct);
                    db.SaveChanges();
                }
                else if (bayToUpdate == null)
                {
                    Bay productDeleted = new Bay();
                    TryUpdateModel(productDeleted, fieldsToBind);
                    ModelState.AddModelError(string.Empty, "Unable to save changes. The product was deleted by another user.");
                    //return View(productDeleted);
                    return(RedirectToAction("Index"));
                }
                if (bayToUpdate != null)
                {
                    TryUpdateModel(bayToUpdate, fieldsToBind);
                    string UpdatedUser = string.Empty;
                    try
                    {
                        //string strDDLValue = Request.Form["ddlVendor"].ToString();
                        bayToUpdate.ProductID    = Convert.ToInt32(ddlProduct[0]); //Convert.ToInt32(Request["ddlProduct"].ToString());
                        UpdatedUser              = bayToUpdate.ModifiedBy;
                        bayToUpdate.ModifiedDate = DateTime.Now;
                        bayToUpdate.ModifiedBy   = User.Identity.Name;
                        db.Entry(bayToUpdate).Property(x => x.CreatedDate).IsModified = false;
                        db.Entry(bayToUpdate).Property(x => x.CreatedBy).IsModified   = false;
                        db.Entry <Bay>(bayToUpdate).State = EntityState.Modified;
                        db.Entry(bayToUpdate).OriginalValues["RowVersion"] = RowVersion;
                        await db.SaveChangesAsync();

                        return(RedirectToAction("Index"));
                    }
                    catch (DbUpdateConcurrencyException ex)
                    {
                        var entry         = ex.Entries.Single();
                        var clientValues  = (Bay)entry.Entity;
                        var databaseEntry = entry.GetDatabaseValues();
                        if (databaseEntry == null)
                        {
                            TempData["ErrorMessage"] = "Unable to save changes. The record was deleted";
                        }
                        else
                        {
                            TempData["ErrorMessage"] = "Unable to save changes. The record was edited by " + UpdatedUser + ". Select Updated Record.";
                        }
                    }
                }
                //bay.ModifiedDate = DateTime.Now;
                //bay.ModifiedBy = User.Identity.Name;
                //db.Entry(bay).State = EntityState.Modified;
                //db.Entry(bay).Property(x => x.CreatedDate).IsModified = false;
                //db.Entry(bay).Property(x => x.CreatedBy).IsModified = false;
                //db.SaveChanges();
                return(RedirectToAction("Index"));
            }
            //ViewBag.ProductID = new SelectList(db.Products, "ProductID", "ProductName", bay.ProductID);
            //return View(bay);
            return(RedirectToAction("Index"));
        }