0

I´m following this tutorial from Learn Microsoft and I have a problem I don´t know why... I just followed Step by Step (just change project name and context) but doesn´t work for me. Everything is fine until I have this piece of code in the Create.cshtml.cs modify part

public async Task<IActionResult> OnPostAsync()
{
    var emptyStudent = new Student();

    if (await TryUpdateModelAsync<Student>(
        emptyStudent,
        "student",   // Prefix for form value.
        s => s.LastName, s => s.FirstMidName, s => s.EnrollmentDate))
    {
        _context.Students.Add(emptyStudent);
        await _context.SaveChangesAsync();
        return RedirectToPage("./Index");
    }
           
    return Page();
}

TryUpdateModelAsync doesn´t validate the model so I can´t save the new student. If I debbug the app, the request gets the values from the form (this/Request/Form), this image shows debbug output at TryUpdateModelAsync breakpoint.

If I don´t use TryUpdateModelAsync, just doing this

var emptyStudent = new Student();
emptyStudent = Student;
_context.Students.Add(emptyStudent);
await _context.SaveChangesAsync();
return RedirectToPage("./Index");

It works perfectly but there is no overposting filter. The complete project is here in my github. Can anybody give me a push in what´s happening? Big thanks in advance!

codeMonkey
  • 4,134
  • 2
  • 31
  • 50
datecasp
  • 1
  • 2

0 Answers0