コード例 #1
0
ファイル: Edit.cshtml.cs プロジェクト: sonercanoglu/CoreRazor
        public async Task <IActionResult> OnGetAsync(int?id)
        {
            if (id == null)
            {
                return(NotFound());
            }

            try
            {
                brandModel = await _context.BrandModels.Where(m => m.Id == id).FirstOrDefaultAsync();

                if (brandModel == null)
                {
                    return(NotFound());
                }

                ViewData["BrandList"] = new SelectList(_context.Brands, "Id", "Name");

                return(Page());
            }
            catch (Exception ex)
            {
                //With this code, we save in log files what we want.
                _log.LogError(ex.Message + ". ID : {0}", id);
                TempData["ErrorMessage"] = ex.Message;

                return(RedirectToPage("Error"));
            }
        }
コード例 #2
0
        public IActionResult OnGet()
        {
            ViewData["BrandList"] = new SelectList(_context.Brands, "Id", "Name");

            brandModel = new Models.BrandModel();

            return(Page());
        }
コード例 #3
0
        public async Task <IActionResult> Index(Models.BrandModel model)
        {
            if (ModelState.IsValid)
            {
                //save new brand to the database
                dbContext.Brands.Add(model);
                await dbContext.SaveChangesAsync();

                //redirect to the main view
                return(RedirectToAction("Index"));
            }
            else
            {
                return(View());
            }
        }
コード例 #4
0
        public async Task <IActionResult> OnPostDeleteAsync(string deleteId)
        {
            int id = 0;

            if (!int.TryParse(deleteId, out id))
            {
                throw new Exception("Wrong Id Information.");
            }

            try
            {
                Models.BrandModel brandModel = await _context.BrandModels.Where(m => m.Id == id).FirstOrDefaultAsync();

                if (brandModel == null)
                {
                    return(NotFound());
                }

                if (brandModel.Products != null && brandModel.Products.Count != 0)
                {
                    throw new Exception("There are related Records with this Brand. First You have to delete them!");
                }

                _context.BrandModels.Remove(brandModel);

                //With this code, we save the entity history.
                _context.EnsureAutoHistory();
                await _context.SaveChangesAsync();

                //With this code, we save in log files what we want.
                _log.LogInformation("Record Deleted");

                return(RedirectToPage("Index", new { messageType = MessageType.Delete }));
            }
            catch (Exception ex)
            {
                //With this code, we save in log files what we want.
                _log.LogError(ex.Message + ". ID : {0}", id);
                TempData["ErrorMessage"] = ex.Message;

                return(RedirectToPage("Error"));
            }
        }