Пример #1
0
        public async Task <IActionResult> Save(QuoteCategory category)
        {
            try
            {
                if (!ModelState.IsValid)
                {
                    return(View("Form", category));
                }

                if (category.Id == 0)
                {
                    _context.Add(category);
                }
                else
                {
                    var categoriesInDb = await _context.QuoteCategory.SingleOrDefaultAsync(c => c.Id == category.Id);

                    categoriesInDb.Name = category.Name;
                }
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateException)
            {
                ModelState.AddModelError("", "Unable to save changes. " +
                                         "Try again, and if the problem persists " +
                                         "see your system administrator.");
            }

            return(RedirectToAction("Index", "Categories"));
        }
Пример #2
0
 public IActionResult AddQuote(QuoteViewModel incoming)
 {
     if (ModelState.IsValid)
     {
         //meta
         Meta newMeta = new Meta();
         newMeta.notes = incoming.meta;
         _context.metas.Add(newMeta);
         _context.SaveChanges();
         //reassign to db instance of meta
         newMeta = _context.metas.Last();
         //quote
         Quote  newQuote = new Quote();
         Author auth     = _context.authors.SingleOrDefault(Author => Author.authorid == incoming.authorid);
         newQuote.author = auth;
         newQuote.quote  = incoming.quote;
         newQuote.meta   = newMeta;
         _context.quotes.Add(newQuote);
         _context.SaveChanges();
         // reassign newQuote to db instance
         newQuote = _context.quotes.Last();
         //quotecats
         QuoteCategory newQcat = new QuoteCategory();
         Category      cat     = _context.categories.SingleOrDefault(Category => Category.categoryid == incoming.categoryid);
         newQcat.category = cat;
         newQcat.quote    = newQuote;
         _context.quotecategories.Add(newQcat);
         _context.SaveChanges();
         System.Console.WriteLine("Goodness!");
     }
     else
     {
         System.Console.WriteLine("Badness. :(");
     }
     return(RedirectToAction("Index"));
 }
Пример #3
0
        public IActionResult New()
        {
            var category = new QuoteCategory();

            return(View("Form", category));
        }