Пример #1
0
        public async Task <IActionResult> CreateLsVaoRa(LichSuVaoRa ls)
        {
            List <string> message = new List <string>();

            try
            {
                if (!ModelState.IsValid)
                {
                    throw new Exception("Thông tin không hợp lệ");
                }
                await _context.LichSuVaoRa.AddAsync(ls);

                message.Add("redirect");
                message.Add(Url.Action("LsVaoRa"));
            }
            catch (Exception ex)
            {
                message.Add(ex.Message);
                foreach (var value in ModelState.Values)
                {
                    if (value.ValidationState == ModelValidationState.Invalid)
                    {
                        foreach (var modelErr in value.Errors)
                        {
                            message.Add(modelErr.ErrorMessage);
                        }
                    }
                }
            }
            return(Json(message.ToArray()));
        }
        public IActionResult LsVaoRa()
        {
            LichSuVaoRa lastRecord = _context.LichSuVaoRa.LastOrDefault();
            int         lastStt    = 0;

            if (lastRecord != null)
            {
                lastStt = lastRecord.Stt;
            }
            ViewBag.NextStt = lastStt + 1;
            return(View(_context.LichSuVaoRa.ToArray()));
        }
        public async Task <IActionResult> DeleteLsVaoRa(LichSuVaoRa vaoRa)
        {
            List <string> messages = new List <string>();

            try
            {
                if (!ModelState.IsValid)
                {
                    throw new Exception("Thông tin không hợp lệ");
                }
                LichSuVaoRa _vaoRa = await _context.LichSuVaoRa.FindAsync(vaoRa.Stt);

                if (_vaoRa == null)
                {
                    throw new Exception("Không tìm thấy bản ghi vào-ra");
                }
                _context.LichSuVaoRa.Remove(_vaoRa);
                _context.SaveChanges();
                messages.Add("redirect");
                messages.Add(Url.Action("LsVaoRa"));
            }
            catch (Exception err)
            {
                messages.Add(err.Message);
                foreach (var value in ModelState.Values)
                {
                    if (value.ValidationState == ModelValidationState.Invalid)
                    {
                        foreach (var modelErr in value.Errors)
                        {
                            messages.Add(modelErr.ErrorMessage);
                        }
                    }
                }
            }
            return(Json(messages.ToArray()));
        }