-
Notifications
You must be signed in to change notification settings - Fork 25.1k
Open
Labels
⌚ Not TriagedSource - Docs.msDocs Customer feedback via GitHub IssueDocs Customer feedback via GitHub Issueaspnet-core/svcdata-access/subsvc
Description
Description
In the EditPost
action method, the DbUpdateException
is caught to add a ModelState error. To display this error to the user, the code should re-render the same Edit view. But the code redirects the user to the Index page. So the error is never displayed to the user.
[HttpPost, ActionName("Edit")]
[ValidateAntiForgeryToken]
public async Task<IActionResult> EditPost(int? id)
{
if (id == null)
{
return NotFound();
}
var courseToUpdate = await _context.Courses
.FirstOrDefaultAsync(c => c.CourseID == id);
if (await TryUpdateModelAsync<Course>(courseToUpdate,
"",
c => c.Credits, c => c.DepartmentID, c => c.Title))
{
try
{
await _context.SaveChangesAsync();
}
// ------------------------------------------------ Start Issue
catch (DbUpdateException /* ex */)
{
//Log the error (uncomment ex variable name and write a log.)
ModelState.AddModelError("", "Unable to save changes. " +
"Try again, and if the problem persists, " +
"see your system administrator.");
}
return RedirectToAction(nameof(Index));
// ------------------------------------------------ End Issue
}
PopulateDepartmentsDropDownList(courseToUpdate.DepartmentID);
return View(courseToUpdate);
}
Page URL
https://learn.microsoft.com/en-us/aspnet/core/data/ef-mvc/update-related-data?view=aspnetcore-8.0
Content source URL
https://github.com/dotnet/AspNetCore.Docs/blob/main/aspnetcore/data/ef-mvc/update-related-data.md
Document ID
c06d8343-1407-aa05-de96-c1aca71ba232
Article author
Metadata
Metadata
Assignees
Labels
⌚ Not TriagedSource - Docs.msDocs Customer feedback via GitHub IssueDocs Customer feedback via GitHub Issueaspnet-core/svcdata-access/subsvc