public async Task <IActionResult> OnPost() { // Check if the provided model isn't valid. if (!ModelState.IsValid) { // Add an error to the model. ModelState.AddModelError(string.Empty, "An error has been encountered. Please check again the input fields."); // Redisplay the page. return(Page()); } // Define a new task. var task = new DatabaseTypesTask { Items = new List <DatabaseTypeInputModel> { new DatabaseTypeInputModel { Name = Input.Name, Description = Input.Description } } }; // Try to run the task. try { // Run the task. await task.CreateAsync(_serviceProvider, CancellationToken.None); } catch (Exception exception) { // Add an error to the model. ModelState.AddModelError(string.Empty, exception.Message); // Redisplay the page. return(Page()); } // Display a message. TempData["StatusMessage"] = "Success: 1 database type created successfully."; // Redirect to the index page. return(RedirectToPage("/Administration/Databases/DatabaseTypes/Index")); }
public async Task <IActionResult> OnPost() { // Check if there isn't any ID provided. if (string.IsNullOrEmpty(Input.Id)) { // Display a message. TempData["StatusMessage"] = "Error: No ID has been provided."; // Redirect to the index page. return(RedirectToPage("/Administration/Databases/DatabaseTypes/Index")); } // Define the query. var query = _context.DatabaseTypes .Where(item => item.Id == Input.Id); // Define the view. View = new ViewModel { DatabaseType = query .FirstOrDefault() }; // Check if the item hasn't been found. if (View.DatabaseType == null) { // Display a message. TempData["StatusMessage"] = "Error: No item could be found with the provided ID."; // Redirect to the index page. return(RedirectToPage("/Administration/Databases/DatabaseTypes/Index")); } // Check if the database type is the generic database type. if (View.DatabaseType.Name == "Generic") { // Display a message. TempData["StatusMessage"] = "Error: The generic database type can't be edited."; // Redirect to the index page. return(RedirectToPage("/Administration/Databases/DatabaseTypes/Index")); } // Check if the provided model isn't valid. if (!ModelState.IsValid) { // Add an error to the model. ModelState.AddModelError(string.Empty, "An error has been encountered. Please check again the input fields."); // Redisplay the page. return(Page()); } // Define a new task. var task = new DatabaseTypesTask { Items = new List <DatabaseTypeInputModel> { new DatabaseTypeInputModel { Id = Input.Id, Name = Input.Name, Description = Input.Description } } }; // Try to run the task. try { // Run the task. await task.EditAsync(_serviceProvider, CancellationToken.None); } catch (Exception exception) { // Add an error to the model. ModelState.AddModelError(string.Empty, exception.Message); // Redisplay the page. return(Page()); } // Display a message. TempData["StatusMessage"] = "Success: 1 database type updated successfully."; // Redirect to the index page. return(RedirectToPage("/Administration/Databases/DatabaseTypes/Index")); }