コード例 #1
0
 // Lägger till en medlem i databasen.
 public void ContactListView_InsertItem(Contact contact)
 {
     if (ModelState.IsValid)
     {
         try
         {
             // Lägger till kontakten i databasen, samt presenterar ett meddelande om att allt lyckats.
             Service.SaveContact(contact);
             Session["Success"] = "Kontakten lades till!";
             Response.Redirect(Request.UrlReferrer.ToString());
         }
         catch (Exception)
         {
             ModelState.AddModelError("", "Ett fel inträffade då kontakten skulle läggas till.");
         }
     }
 }
コード例 #2
0
        // Sparar en kontakt, endera ny eller med uppdaterad information.
        public void SaveContact(Contact contact)
        {
            ICollection<ValidationResult> validationResults;
            if (!contact.Validate(out validationResults))
            {
                var ex = new ValidationException("Kontaktobjektet klarade inte datavalideringen.");
                ex.Data.Add("ValidationResults", validationResults);
                throw ex;
            }

            if (contact.ContactID == 0)
            {
                ContactDAL.InsertContact(contact);
            }
            else
            {
                ContactDAL.UpdateContact(contact);
            }
        }
コード例 #3
0
        public ActionResult DeleteConfirmed(int id=0)
        {
            try
             {
                 var contactToDelete = new Contact { ContactID = id };
                 _repository.Delete(contactToDelete);
                 _repository.Save();
                 TempData["sucess"] = TempData["sucess"] = "Contact deleted.";

                 return RedirectToAction("Index");
             }
             catch (DataException)
             {
                  TempData["error"] = "Failed to delete contact.";

             }

             return RedirectToAction("Delete", new { id = id });
        }
コード例 #4
0
        public ActionResult Edit(Contact contact)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    _repository.Update(contact);
                    _repository.Save();

                    return RedirectToAction("Index");
                }
            }
            catch (Exception)
            {
                ModelState.AddModelError(String.Empty, "Create was unsuccessful. Please correct any errors and try again.");
            }

            return View("Edit", contact);
        }
コード例 #5
0
 public ActionResult Edit(Contact contact)
 {
     try
     {
         if (ModelState.IsValid)
         {
             _repository.Update(contact);
             _repository.Save();
             return View("Success", contact);
         }
         throw new ApplicationException("Ett okänt fel inträffade");
     }
     catch (Exception e)
     {
         return View("NotFound", string.Empty, e.Message);
     }
 }
        public ActionResult Edit(Contact contact)
        {
            if (ModelState.IsValid)
            {
                try
                {
                    _repository.Update(contact);
                    _repository.Save();

                    return RedirectToAction("Success");
                }
                catch (Exception)
                {
                    ModelState.AddModelError(String.Empty, "Ett fel inträffade vid uppdateringen!");
                }
            }
            else
            {
                return View("Edit", contact);
            }
            return View("Index", _repository.GetLastContacts());
        }