コード例 #1
0
        public async Task <IActionResult> Create([Bind("Id,Name")] LightFood lightFood)
        {
            if (!ModelState.IsValid)
            {
                string msg = (ModelState.FirstOrDefault(x => x.Value.Errors.Any()).Value.Errors.FirstOrDefault().ErrorMessage).Replace("'", "");
                _toastNotification.AddErrorToastMessage(msg);

                return(View(lightFood));
            }
            if (ModelState.IsValid)
            {
                var checkExit = _context.LightFood.Where(x => x.Name.ToLower() == lightFood.Name.ToLower()).Count();

                if (checkExit == 0)
                {
                    _context.Add(lightFood);
                    await _context.SaveChangesAsync();

                    _toastNotification.AddErrorToastMessage(ResponseMessageUtilities.CREATED_SUCESSFUL);
                    return(RedirectToAction(nameof(Index)));
                }
                _toastNotification.AddErrorToastMessage(ResponseMessageUtilities.ITEM_EXIST);
                return(RedirectToAction(nameof(Index)));
            }

            return(View(lightFood));
        }
コード例 #2
0
        public async Task <IActionResult> Edit(int id, [Bind("Id,Name")] LightFood lightFood)
        {
            if (!ModelState.IsValid)
            {
                string msg = (ModelState.FirstOrDefault(x => x.Value.Errors.Any()).Value.Errors.FirstOrDefault().ErrorMessage).Replace("'", "");
                _toastNotification.AddErrorToastMessage(msg);

                return(View(lightFood));
            }

            var CheckExist = _context.LightFood.Where(x => x.Id != lightFood.Id && x.Name.ToLower() == lightFood.Name.ToLower()).Count();

            if (CheckExist == 0)
            {
                var model = _context.LightFood.FirstOrDefault(x => x.Id == lightFood.Id);
                model.Name = lightFood.Name;
                await _context.SaveChangesAsync();

                _toastNotification.AddSuccessToastMessage(ResponseMessageUtilities.UPDATE_SUCESSFUL);

                return(RedirectToAction(nameof(Index)));
            }
            _toastNotification.AddErrorToastMessage(ResponseMessageUtilities.ITEM_EXIST);
            return(View(lightFood));
        }