Exemplo n.º 1
0
        public ActionResult New()
        {
            var membershipTypes = _context.MemberShipTypes.ToList();
            var viewModel       = new NewCustViewModel
            {
                MemberShipTypes = membershipTypes
            };

            return(View(viewModel));
        }
        public ActionResult Save(Customer customer)
        {
            HttpResponseMessage mresponse = GlobalVariables.webApiClient.GetAsync("MemberShipApi").Result;
            var membershipTypes           = mresponse.Content.ReadAsAsync <IEnumerable <MemberShipType> >().Result;
            HttpResponseMessage cresponse = GlobalVariables.webApiClient.GetAsync("Customers").Result;

            if (!ModelState.IsValid)
            {
                var viewModel = new NewCustViewModel
                {
                    MemberShipTypes = membershipTypes
                };

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

            if (customer.Id == 0)
            {
                cresponse = GlobalVariables.webApiClient.PostAsJsonAsync("Customers", customer).Result;
            }
            else
            {
                cresponse = GlobalVariables.webApiClient.PutAsJsonAsync($"Customers/{customer.Id}", customer).Result;
            }

            return(RedirectToAction("Index"));
            //if (!ModelState.IsValid)
            //{
            //    var viewModel = new NewCustViewModel
            //    {
            //        Customer = customer,
            //        MemberShipTypes = _context.MemberShipTypes.ToList()

            //    };
            //    return View("New",viewModel);
            //}
            //if (customer.Id == 0)
            //    _context.Customers.Add(customer);
            //else
            //{
            //var customerInDb = _context.Customers.Single(c => c.Id == customer.Id);
            // customerInDb.BirthDate = customer.BirthDate;
            //    customerInDb.MembershipTypeId = customer.MembershipTypeId;
            //    customerInDb.IsSubscribedToNewsLetter = customer.IsSubscribedToNewsLetter;

            //}
            //_context.SaveChanges();
            //return RedirectToAction("Index", "Customer");
        }
        public ActionResult New()
        {
            //var membershipTypes = _context.MemberShipTypes.ToList();
            //var ViewModel = new NewCustViewModel
            //{
            //    MemberShipTypes = membershipTypes
            //};
            //        return View(ViewModel);
            HttpResponseMessage mresponse = GlobalVariables.webApiClient.GetAsync("MemberShipApi").Result;

            var membershipTypes = mresponse.Content.ReadAsAsync <IEnumerable <MemberShipType> >().Result;

            var viewModel = new NewCustViewModel

            {
                Customer        = new Customer(),
                MemberShipTypes = membershipTypes
            };

            return(View("New", viewModel));
        }
        public ActionResult Edit(int id)
        {
            HttpResponseMessage mr = GlobalVariables.webApiClient.GetAsync("MemberShipApi").Result;
            HttpResponseMessage cr = (GlobalVariables.webApiClient.GetAsync($"Customer/{id}")).Result;
            var vm = new NewCustViewModel
            {
                Customer        = cr.Content.ReadAsAsync <Customer>().Result,
                MemberShipTypes = mr.Content.ReadAsAsync <IEnumerable <MemberShipType> >().Result
            };

            return(View("new", vm));
            //var updateCast = _context.Customers.SingleOrDefault(c => c.Id == id);
            //if (updateCast == null)
            //{
            //    return HttpNotFound();
            //}
            //var vm = new NewCustViewModel
            //{
            //    Customer = updateCast,
            //    MemberShipTypes = _context.MemberShipTypes.ToList()
            //};
            //return View("New", vm);
        }