Code to reproduce the bug at https://github.com/Rick-Anderson/TryUpdateModelAsync
To reproduce the bug:
http://localhost:xxx/Students)The problem is passing in an empty prefix. The prefix used here should be "Student" or "student" to match the <input/>s in the form.
@dougbu changed to Student - no joy
This is exactly the same code used in a MVC controller, and no prefix was used there.
I made the following two one line changes and everything lit up.
--- a/ContosoUniversity/ContosoUniversity/Pages/Students/Create2.cshtml.cs
+++ b/ContosoUniversity/ContosoUniversity/Pages/Students/Create2.cshtml.cs
@@ -42,11 +42,12 @@ namespace ContosoUniversity.Pages.Students
if (await TryUpdateModelAsync<Student>(
emptyStudent,
- "",
+ "student",
s => s.FirstMidName, s => s.LastName, s => s.EnrollmentDate))
{
try
{
+ _context.Students.Add(emptyStudent);
await _context.SaveChangesAsync();
return RedirectToPage("./Index");
Yeah, Razor Pages tutorial has slightly different code (contains prefix)
Most helpful comment
I made the following two one line changes and everything lit up.