Example #1
0
        public ActionResult Edit(int?id)
        {
            PhonesService phonesService = new PhonesService();
            PhonesEditVM  model         = new PhonesEditVM();

            TryUpdateModel(model);

            Phone phone;

            if (!id.HasValue)
            {
                phone = new Phone();
            }
            else
            {
                phone = phonesService.GetByID(id.Value);
                if (phone == null)
                {
                    if (phonesService.GetContact(model.ContactID) == null)
                    {
                        return(this.RedirectToAction <ContactsController>(c => c.List()));
                    }

                    return(this.RedirectToAction(c => c.List(), new { ContactID = model.ContactID }));
                }
                model.ContactID = phone.ContactID;
            }

            Mapper.Map(phone, model);

            return(View(model));
        }
Example #2
0
        public ActionResult Edit()
        {
            PhonesService phonesService = new PhonesService();
            PhonesEditVM  model         = new PhonesEditVM();

            TryUpdateModel(model);

            if (phonesService.GetContact(model.ContactID) == null)
            {
                return(this.RedirectToAction <ContactsController>(c => c.List()));
            }

            Phone phone;

            if (model.ID == 0)
            {
                phone = new Phone();
            }
            else
            {
                phone = phonesService.GetByID(model.ID);
                if (phone == null)
                {
                    return(this.RedirectToAction(c => c.List(), new { ContactID = model.ContactID }));
                }
            }

            if (!ModelState.IsValid)
            {
                return(View(model));
            }

            Mapper.Map(model, phone);

            phonesService.Save(phone);

            return(this.RedirectToAction(c => c.List(), new { ContactID = model.ContactID }));
        }