コード例 #1
0
 public async Task<IActionResult> Add()
 {
     var model = new AuthorViewModel();
     ViewBag.Title = "Add";
     await SetViewBagAuthors();
     return View(model);
 }
コード例 #2
0
        public async Task<IActionResult> Add(AuthorViewModel model)
        {
            if (ModelState.IsValid)
            {
                var newModel = Mapper.Map(model, new Author(), m => new { m.Name, m.Slug, m.AppUserId, m.ShortDescription, m.Content });

                newModel.CreatedOn = DateTime.Now;
                newModel.CreatedBy = AppSession.AppUserId;

                Db.Authors.Add(newModel);
                await Db.SaveChangesAsync();
                return RedirectToAction("Index");
            }
            //TempData.Danger("Author was not saved! Please check errors.");

            ViewBag.Title = "Add";
            await SetViewBagAuthors();
            return View(model);
        }
コード例 #3
0
        public async Task<IActionResult> Delete(AuthorViewModel model)
        {
            if (ModelState.IsValid)
            {
                // Can we just update DB without fetching first, need ideas, also concurrency check needs to be done
                var dbModel = await Db.Authors.SingleOrDefaultAsync(d => d.Id == model.Id);
                if (dbModel == null) return HttpNotFound();

                // Need to fix RowStamp check
                //Db.Entry(dbModel).OriginalValues["RowStamp"] = model.RowStamp;
                Db.Authors.Remove(dbModel);
                await Db.SaveChangesAsync();

                //TempData.Success("Author was successfully deleted!");
                return RedirectToAction("Index");
            }
            //TempData.Danger("Author was not deleted! Please check errors.");

            ViewBag.Title = "Delete";
            return View(model);
        }
コード例 #4
0
        public async Task<IActionResult> Edit(AuthorViewModel model)
        {
            if (ModelState.IsValid)
            {
                // Can we just update DB without fetching first, need ideas, also concurrency check needs to be done
                var dbModel = await Db.Authors.FirstAsync(c => c.Id == model.Id);
                Mapper.Map(model, dbModel, m => new { m.Name, m.Slug, m.AppUserId, m.ShortDescription, m.Content });

                dbModel.ModifiedOn = DateTime.Now;
                dbModel.ModifiedBy = AppSession.AppUserId;

                // Need to fix RowStamp check
                //Db.Entry(dbModel).OriginalValues["RowStamp"] = model.RowStamp;
                Db.Update(dbModel);
                await Db.SaveChangesAsync();

                //TempData.Success("Author was successfully saved!");
                return RedirectToAction("Index");
            }
            //TempData.Danger("Author was not saved! Please check errors.");

            ViewBag.Title = "Edit";
            await SetViewBagAuthors();
            return View("Add", model);
        }