Ejemplo n.º 1
0
        public ActionResult Edit(int id)
        {
            var customer = Context.Customers.Include(c => c.MembershipType).FirstOrDefault(x => x.Id == id);

            if (customer == null)
            {
                return(HttpNotFound());
            }
            var membershipTypes   = Context.MembershipTypes.ToList();
            var customerViewModel = new CustomerFormViewModel
            {
                Customer        = customer,
                MembershipTypes = membershipTypes
            };

            return(View("CustomerForm", customerViewModel));
        }
Ejemplo n.º 2
0
        public ActionResult Edit(int id)
        {
            var customer = _customerService.CustomerById(id);

            if (customer == null)
            {
                return(HttpNotFound());
            }

            var viewModel = new CustomerFormViewModel
            {
                Customer        = customer,
                MembershipTypes = _customerService.MembershipTypes()
            };

            return(View("CustomerForm", viewModel));
        }
Ejemplo n.º 3
0
        public ActionResult Edit(int id)
        {
            var customer = _dbContext.Customers.SingleOrDefault(w => w.Id == id);

            if (customer == null)
            {
                return(HttpNotFound());
            }

            var newVM = new CustomerFormViewModel
            {
                Customer        = customer,
                MembershipTypes = _dbContext.MembershipTypes.ToList()
            };

            return(View("CustomerForm", newVM));
        }
Ejemplo n.º 4
0
        public ActionResult Edit(int id)
        {
            var customer = _context.Customers.SingleOrDefault(c => c.Id == id);

            if (customer == null)
            {
                return(HttpNotFound());
            }

            var viewModel = new CustomerFormViewModel
            {
                Customer       = customer,
                MembershipType = _context.MembershipType.ToList()
            };

            return(View("CustomerForm", viewModel));  //without this, MVC will look for 'Edit' view
        }
Ejemplo n.º 5
0
        public ActionResult Edit(int id)
        {
            var customer = _context.Customers.SingleOrDefault(c => c.Id == id);

            if (customer == null)
            {
                return(HttpNotFound());
            }

            var viewModel = new CustomerFormViewModel()
            {
                Customer        = customer,
                MembershipTypes = _context.MembershipTypes.ToList()
            };

            return(View("CustomerForm", viewModel));
        }
Ejemplo n.º 6
0
        public ActionResult Edit(int id)
        {
            var customer = _context.Customers.SingleOrDefault(c => c.Id == id);

            if (customer == null)
            {
                return(HttpNotFound());
            }

            var viewModel = new CustomerFormViewModel
            {
                Customer        = customer,
                MembershipTypes = _context.MembershipTypes.ToList()
            };

            return(View("CustomerForm", viewModel)); //need to override here otherwise MVC will look for a view called Edit
        }
Ejemplo n.º 7
0
        public ActionResult Edit(int customerId)
        {
            var customer = _context.Customers.FirstOrDefault(c => c.Id == customerId);

            if (customer == null)
            {
                return(HttpNotFound());
            }

            var newCustomerViewModel = new CustomerFormViewModel
            {
                Customer        = customer,
                MembershipTypes = _context.MembershipTypes.ToList()
            };

            return(View("CustomerForm", newCustomerViewModel));
        }
Ejemplo n.º 8
0
        public IActionResult Update(int id)                             /* /Customers/Details/id update customer information */
        {
            try
            {
                CustomerFormViewModel model = new CustomerFormViewModel(Logger, id);
                return(View("CustomerForm", model));
            }
            catch (Exception exception)
            {
                Debug.Assert(false, "CustomerFormViewModel Couldn't Locate Customer(id)");
                Debug.Assert(false, exception.Message);

                Logger.LogError(exception, "Exception in CustomersController:Update. Directing user with AppError result", null);

                return(RedirectToAction("ApplicationError", "Abort"));
            }
        }
        public ActionResult Save(Customer customer)
        {
            //[Bind(Exclude = "Id")] not gonna work for update
//            ModelState.Remove("Id");
            if (!ModelState.IsValid)
            {
//                var errors = ModelState.SelectMany(x => x.Value.Errors.Select(z => z.Exception));

                var viewModel = new CustomerFormViewModel()
                {
                    Customer        = customer,
                    MemberShipTypes = _context.MemberShipTypes.ToList()
                };
                return(View("CustomerForm", viewModel));
            }


            if (customer.Id == 0)
            {
                _context.Customers.Add(customer);
            }
            else
            {
                var customerInDb = _context.Customers.Find(customer.Id);
                customerInDb.Name                     = customer.Name;
                customerInDb.BirthDate                = customer.BirthDate;
                customerInDb.MemberShipTypeId         = customer.MemberShipTypeId;
                customerInDb.IsSubscribedToNewsLetter = customer.IsSubscribedToNewsLetter;
            }

            _context.SaveChanges();

            return(RedirectToAction("Index", "Customer"));

//            //ViewBag.Message = aCustomerManager.Save(customer);
//            var viewModel2 = new CustomerFormViewModel
//            {
//                MemberShipTypes = _context.MemberShipTypes.ToList()
//            };
//
//            ModelState.Clear();
//            return View("CustomerForm", viewModel2);


            // return RedirectToAction("Index","Customer");
        }
Ejemplo n.º 10
0
        public IActionResult Edit(int id)
        {
            var customer = _MovieRateContext.Customers.SingleOrDefault(c => c.Id == id);

            if (customer == null)
            {
                return(StatusCode(404));
            }

            var viewModel = new CustomerFormViewModel
            {
                Customer        = customer,
                MembershipTypes = _MovieRateContext.MemberShipTypes.ToList()
            };

            return(View("CustomerForm", viewModel));
        }
Ejemplo n.º 11
0
        public ActionResult Edit(int Id)
        {
            var customer = _context.Customers.SingleOrDefault(c => c.Id == Id);

            if (customer == null)
            {
                return(HttpNotFound());
            }
            var viewModel = new CustomerFormViewModel
            {
                //it filled the Textboxes, because i sent the customer to Model
                Customer        = customer,
                MembershipTypes = _context.MembershipTypes.ToList()
            };

            return(View("CustomerForm", viewModel));
        }
Ejemplo n.º 12
0
        public ActionResult Edit(int id)
        {
            using (var context = new ApplicationDbContext())
            {
                var membershipTypes = context.MembershipTypes.ToList();
                var customer        = context.Customers.FirstOrDefault(p => p.Id == id);

                var viewModel = new CustomerFormViewModel()
                {
                    MembershipTypes = membershipTypes,
                    Customer        = customer
                };

                ViewBag.FormTitle = "Edit customer";
                return(View("CustomerForm", viewModel));
            };
        }
Ejemplo n.º 13
0
        public ActionResult Edit(int?custID)
        {
            var lstCustomer = _myDBContext.Customers.FirstOrDefault(t => t.Id == custID);

            if (lstCustomer == null)
            {
                return(HttpNotFound());
            }

            var customerFormViewModel = new CustomerFormViewModel
            {
                Customer        = lstCustomer,
                MembershipTypes = _myDBContext.MembershipTypes.ToList()
            };

            return(View("CustomerForm", customerFormViewModel));
        }
Ejemplo n.º 14
0
        public ActionResult New()
        {
            // Initializing MembershipTypes used for the Form's DropDown ...
            var membershipTypes = _context.MembershipTypes.ToList();


            var viewModel = new CustomerFormViewModel
            {
                // 20190331 Initializing (to default values) CustomerFormViewModel-Customer to be sure the hidden Customer.Id which is an int (will be initialized to 0)
                // 20190331 in CustomerForm view is 0 for New customers which is used in the Save view to know if New or Edit ..
                Customer        = new Customer(),
                MembershipTypes = membershipTypes
            };

            // Override the View to go to "CustomerForm" instead of using the default "New"...
            // return View(viewModel);
            return(View("CustomerForm", viewModel));
        }
Ejemplo n.º 15
0
        public ActionResult Edit(int id)
        {
            var customer = _context.Customers.SingleOrDefault(c => c.Id == id);

            if (customer == null)
            {
                return(HttpNotFound());
            }

            var membershipTypes = _context.MembershipTypes.ToList();
            var viewModel       = new CustomerFormViewModel(customer)
            {
                MembershipTypes = membershipTypes
            };

            //ovveride naming convention for view with existing view name
            return(View("AddEditForm", viewModel));
        }
Ejemplo n.º 16
0
        public void OnFragmentNavigation(FragmentNavigationEventArgs e)
        {
            DataContext = new CustomerFormViewModel();
            var vm = (CustomerFormViewModel)DataContext;

            if (!string.IsNullOrEmpty(e.Fragment))
            {
                var customerId = Parse(e.Fragment);
                if (customerId > 0)
                {
                    vm.Id = customerId;
                }
            }
            else
            {
                vm.Id = null;
            }
        }
Ejemplo n.º 17
0
        public ActionResult Update(CustomerFormViewModel viewModel)
        {
            if (!ModelState.IsValid)
            {
                viewModel.MembershipTypes = _context.MembershipTypes.ToList();
                return(View("CustomerForm", viewModel));
            }

            var customer = _context.Customers.Single(c => c.Id == viewModel.Id);

            customer.DateTime         = viewModel.DateTime;
            customer.MembershipTypeId = viewModel.MembershipTypeId;
            customer.Name             = viewModel.Name;

            _context.SaveChanges();

            return(RedirectToAction("Index", "Customers"));
        }
        public IActionResult Edit(int id)
        {
            var customer = this.customers.ById(id);

            if (customer is null)
            {
                return(NotFound());
            }

            var customerFormViewModel = new CustomerFormViewModel
            {
                Name          = customer.Name,
                BirthDate     = customer.BirthDate,
                IsYoungDriver = customer.IsYoungDriver
            };

            return(View(customerFormViewModel));
        }
Ejemplo n.º 19
0
        public ActionResult Edit(int id)
        {
            var chooseCustomerForUpdate = customer.GetSingleCustomer(id);
            var membershipTypes         = customer.GetAllMemberships();


            if (chooseCustomerForUpdate == null)
            {
                return(View("CustomerNotFound"));
            }

            var viewModel = new CustomerFormViewModel(chooseCustomerForUpdate)
            {
                MembershipTypes = customer.GetAllMemberships()
            };

            return(View("EditCustomerForm", viewModel));
        }
Ejemplo n.º 20
0
        //To edit a customer
        public ActionResult Edit(int id)
        {
            Customer customer = _context.Customers.SingleOrDefault(x => x.Id == id);

            if (customer == null)
            {
                return(HttpNotFound());
            }

            customer.MembershipType = _context.MembershipTypes.FirstOrDefault(x => x.Id == customer.Id);
            CustomerFormViewModel formViewModel = new CustomerFormViewModel
            {
                Customer        = customer,
                MembershipTypes = _context.MembershipTypes
            };

            return(View("CustomerForm", formViewModel));
        }
Ejemplo n.º 21
0
        public async Task <ActionResult> Edit(int id)
        {
            var customerInDB = await _context.Customers.Include(c => c.MembershipType).SingleOrDefaultAsync(c => c.Id == id);

            if (customerInDB == null)
            {
                return(HttpNotFound());
            }

            var membershipTypesInDb = await _context.MembershipTypes.ToListAsync();

            var viewModel = new CustomerFormViewModel();

            Mapper.Map(customerInDB, viewModel);
            viewModel.MembershipTypes = membershipTypesInDb.Select(Mapper.Map <MembershipType, MembershipTypeViewModel>);

            return(View("CustomerForm", viewModel));
        }
Ejemplo n.º 22
0
        //// Method Edit:
        public ActionResult Edit(int id)
        {
            var customer = _context.Customers.SingleOrDefault(c => c.Id == id); //Will return the customer with the specific id.

            if (customer == null)                                               //If customer for null,
            {
                return(HttpNotFound());
            }

            //If customer not null:
            var viewModel = new CustomerFormViewModel
            {
                Customer        = customer,
                MembershipTypes = _context.MembershipTypes.ToList() //We are put the MembershipTypes straight from the database. And to a list.
            };

            return(View("CustomerForm", viewModel)); //In here we are override the default convention in MVC and specify the view name.
        }
Ejemplo n.º 23
0
        public ActionResult Edit(int id)
        {
            Customer customer = db.Customers.Find(id);
            IEnumerable <MembershipType> membershipTypes = db.MembershipTypes.ToList();

            if (customer == null)
            {
                return(HttpNotFound());
            }

            CustomerFormViewModel viewModel = new CustomerFormViewModel()
            {
                Customer        = customer,
                MembershipTypes = membershipTypes
            };

            return(View(viewModel));
        }
Ejemplo n.º 24
0
        public ActionResult Edit(int id)
        {
            var customer = _context.Customers.SingleOrDefault(c => c.Id == id);

            if (customer == null)
            {
                return(HttpNotFound());
            }

            var viewModel = new CustomerFormViewModel
            {
                Customer        = customer,
                MembershipTypes = _context.MembershipTypes.ToList()
            };

            // Display New.cshtml instead of Edit.cshtml by default.
            return(View("CustomerForm", viewModel));
        }
Ejemplo n.º 25
0
        public ActionResult Edit(int id)
        {
            var customer = _context.Customers.SingleOrDefault(c => c.Id == id);

            if (customer == null)
            {
                return(HttpNotFound());
            }
            var viewModel = new CustomerFormViewModel
            {
                Customer        = customer,
                MembershipTypes = _context.MembershipTypes.ToList()
            };

            /*this will return the form with the customer details
             * based on the customerID*/
            return(View("CustomerForm", viewModel));
        }
Ejemplo n.º 26
0
        public ActionResult Add(Customer customer)
        {
            if (!ModelState.IsValid)
            {
                var viewModel = new CustomerFormViewModel
                {
                    Customer       = customer,
                    MemberShipType = _context.memberShipTypes.ToList()
                };
                return(View("CustomerForm", viewModel));
            }

            if (customer.Id == 0)
            {
                _context.customers.Add(customer);
            }
            else
            {
                var c = _context.customers.Single(m => m.Id == customer.Id);
                c.Name = customer.Name;
                if (customer.DOB == null)
                {
                    c.DOB = null;
                }
                else
                {
                    c.DOB = Convert.ToDateTime(customer.DOB.ToString());
                }

                c.MemberShipTypeId        = customer.MemberShipTypeId;
                c.IsSubscribeToNewsLetter = customer.IsSubscribeToNewsLetter;
            }

            try
            {
                _context.SaveChanges();
            }
            catch (DbEntityValidationException ex)
            {
                Console.WriteLine(ex);
            }

            return(RedirectToAction("Customer", "Customer"));
        }
        public ActionResult Save(CustomerFormViewModel customer)
        {
            if (!ModelState.IsValid)
            {
                var viewModel = new CustomerFormViewModel
                {
                    Customers       = customer.Customers,
                    MembershipTypes = _context.MembershipTypes.ToList()
                };
                return(View("NewCustomerForm", viewModel));
            }

            if (customer.Customers.Id == 0)
            {
                _context.Customers.Add(customer.Customers);
                _context.SaveChanges();
            }
            else
            {
                var customerInDb = _context.Customers.Single(c => c.Id == customer.Customers.Id);

                customerInDb.Name      = customer.Customers.Name;
                customerInDb.Birthdate = customer.Customers.Birthdate;
                customerInDb.IsSubscribedToNewsLetter = customer.Customers.IsSubscribedToNewsLetter;
                customerInDb.MembershipTypeId         = customer.Customers.MembershipTypeId;

                try
                {
                    _context.SaveChanges();
                }
                catch (DbEntityValidationException ee)
                {
                    foreach (var error in ee.EntityValidationErrors)
                    {
                        foreach (var thisError in error.ValidationErrors)
                        {
                            return(Content(string.Format("{0}: {1}", thisError.PropertyName, thisError.ErrorMessage)));
                        }
                    }
                }
            }

            return(RedirectToAction("Index", "Customers"));
        }
Ejemplo n.º 28
0
        public ActionResult Save(Customer customer)
        {
            if (!ModelState.IsValid)
            {
                var viewModel = new CustomerFormViewModel
                {
                    Customer        = customer,
                    MembershipTypes = _context.MembershipTypes.ToList()
                };
                if (customer.Id == 0)
                {
                    _context.Customers.Add(customer);
                }
                else
                {
                    var customerInDb = _context.Customers.Single(c => c.Id == customer.Id);

                    customerInDb.Name      = customer.Name;
                    customerInDb.Birthdate = customer.Birthdate;
                    customerInDb.IsSubscribedToNewsLetter = customer.IsSubscribedToNewsLetter;
                    customerInDb.MembershipType           = customer.MembershipType;
                }
                _context.SaveChanges();

                return(View("CustomerIndex", "Customers"));
            }
            if (customer.Id == 0)
            {
                _context.Customers.Add(customer);
            }
            else
            {
                var customerInDb = _context.Customers.Single(c => c.Id == customer.Id);

                customerInDb.Name                     = customer.Name;
                customerInDb.Birthdate                = customer.Birthdate;
                customerInDb.MembershipTypeId         = customer.MembershipTypeId;
                customerInDb.IsSubscribedToNewsLetter = customer.IsSubscribedToNewsLetter;
            }
            _context.SaveChanges();

            return(RedirectToAction("CustomerIndex", "Customers"));
        }
Ejemplo n.º 29
0
        //data binding. ASP can extract Customer model out of CustomerFormViewModel sent from the CustomerForm view
        public ActionResult Save(Customer customer) //you can also pass in a 'UpdateCustomerDto' object - partial Customer object that contains the properties to be updated (data transfer object)
        {
            //validation:
            if (!ModelState.IsValid)
            {
                var vm = new CustomerFormViewModel()
                {
                    Customer        = customer,
                    MembershipTypes = _context.MembershipTypes.ToList()
                };
                return(View("CustomerForm", vm));
            }

            //based on if the customer has an id or not, we will decide if this is a new customer or an existing customer
            if (customer.Id == 0)
            {
                _context.Customers.Add(customer);
                _context.SaveChanges();//db context goes through all modified objects, generates sql statements at runtime, and update database
            }
            else
            {
                var customerInDb = _context.Customers.Single(c => c.Id == customer.Id);

                /* approach 1: TryUpdateModel(customerInDb);
                 * - this customerInDb object will be updated based on the key-val pairs in the request data.
                 * - CONS: 1). Open security holes: malicious user can modify request data and add additional k-v pairs in form data.
                 *             This method will successfully update all properties.
                 *         2). The workaround method is to provide a list of properties for updating: TryUpdateModel(customerInDb, "", new String[]{"Name", "Email"});
                 *             The problem with this code is that it hard coded the property names, and it's easy to break if the names are changed.
                 */

                // better approach: manually set the properties to be updated
                customerInDb.Name                     = customer.Name;
                customerInDb.Birthday                 = customer.Birthday;
                customerInDb.MembershipTypeId         = customer.MembershipTypeId;
                customerInDb.IsSubscribedToNewsletter = customer.IsSubscribedToNewsletter;
                //or use a Mapper: Mapper.Map(customer, customerInDb); but here the cutomer should be 'UpdateCustomerDto' obj to avoid the security issue

                _context.SaveChanges();
            }

            return(RedirectToAction("Index", "Customers"));
        }
        public ActionResult New()
        {
            string value       = "01-01-1985";
            object objectValue = (string)value;
            string dt          = DateTime.Now.ToShortDateString();

            DateTime time;
            var      isvalid = DateTime.TryParseExact(Convert.ToString(value), System.Globalization.CultureInfo.CurrentCulture.DateTimeFormat.ShortDatePattern, System.Globalization.CultureInfo.CurrentCulture, System.Globalization.DateTimeStyles.None, out time);

            var membershipTypes = _context.MembershipTypes.ToList();

            var viewModel = new CustomerFormViewModel()
            {
                MembershipTypes = membershipTypes,
                Customer        = new Customer()
            };

            return(View("CustomerForm", viewModel));
        }