示例#1
0
        public ActionResult New(Law law)
        {
            try
            {
                if (!ModelState.IsValid)
                {
                    return(View(law));
                }
                else
                {
                    CivLDbContext dbCivL = GetCivLDbContext();

                    dbCivL.Laws.Add(law);
                    dbCivL.SaveChanges();

                    return(RedirectToAction("Index"));
                }
            }
            catch (Exception ex)
            {
                ErrorDTO errorDTO = new ErrorDTO()
                {
                    Message       = "An error occurred while creating a new law.",
                    MessageDetail = ex.Message
                };

                return(View("Error", errorDTO));
            }
        }
示例#2
0
        public ActionResult Law(int id, Law law)
        {
            try
            {
                if (!ModelState.IsValid)
                {
                    return(View(law));
                }
                else
                {
                    CivLDbContext dbCivL = GetCivLDbContext();

                    Law existingLaw = dbCivL.Laws.Find(law.ID);

                    if (existingLaw == null)
                    {
                        throw new Exception($"Law {law.ID} does not exist.");
                    }
                    else
                    {
                        existingLaw.Name = law.Name;
                        existingLaw.Text = law.Text;

                        dbCivL.SaveChanges();

                        return(RedirectToAction("Index"));
                    }
                }
            }
            catch (Exception ex)
            {
                ErrorDTO errorDTO = new ErrorDTO()
                {
                    Message       = $"An error occurred while trying to save Law {law.ID}.",
                    MessageDetail = ex.Message
                };

                return(View("Error", errorDTO));
            }
        }