示例#1
0
        public async Task <IActionResult> Add(NewDepartmentViewModel formData)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    bool bIfExist = false;
                    var  q        = from c in _db.Departments where c.Title == formData.Title select c;
                    try
                    {
                        q.ToList()[0].Title.ToString();
                        bIfExist = true;
                    }
                    catch { }
                    if (bIfExist == true)
                    {
                        ModelState.AddModelError("Department", $"Can not register duplicate record. {formData.Title} Department is already registered");
                    }
                    else
                    {
                        await _departmentServices.AddDepartmentAsync(new Department
                        {
                            DateTimeAdded    = DateTimeOffset.Now,
                            Title            = formData.Title,
                            DateTimeModified = DateTimeOffset.Now,
                            UserAccount      = User.Identity.Name,
                        });

                        TempData["Message"] = "Department Successfully Added";
                        _logger.LogInformation($"Success: successfully added {formData.Title} department record by user={@User.Identity.Name.Substring(4)}");
                        return(RedirectToAction("add"));
                    }
                }
            }
            catch (ApplicationException error)
            {
                ModelState.AddModelError("Department", $"Failed to register record. {formData.Title} Contact IT ServiceDesk for support.");
                _logger.LogError(
                    error,
                    $"FAIL: failed to register {formData.Title} Department. Internal Application Error; user={@User.Identity.Name.Substring(4)}");
            }
            return(View(formData));
        }
 public NewDepartmentPage()
 {
     InitializeComponent();
     BindingContext = new NewDepartmentViewModel();
 }