I'm having a weird one here. I'm doing Microsoft's Razor and EF tutorial and I don't know what's wrong with this code:
public class Student
{
public int ID { get; set; }
public string LastName { get; set; }
public string FirstMidName { get; set; }
public DateTime EnrollmentDate { get; set; }
public ICollection<Enrollment> Enrollments { get; set; }
}
//CRUD Create Method
public async Task<IActionResult> OnPostAsync()
{
var emptyStudent = new Student();
if (await TryUpdateModelAsync<Student>(
emptyStudent,
"Student",
s => s.FirstMidName,
s => s.LastName,
s => s.EnrollmentDate))
{
_context.Students.Add(emptyStudent);
await _context.SaveChangesAsync();
return RedirectToPage("./Index");
}
return Page();
}
The form is as follows:
<form method="post">
<div asp-validation-summary="ModelOnly" class="text-danger"></div>
<div class="form-group">
<label asp-for="Student.LastName" class="control-label"></label>
<input asp-for="Student.LastName" class="form-control" />
<span asp-validation-for="Student.LastName" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="Student.FirstMidName" class="control-label"></label>
<input asp-for="Student.FirstMidName" class="form-control" />
<span asp-validation-for="Student.FirstMidName" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="Student.EnrollmentDate" class="control-label"></label>
<input asp-for="Student.EnrollmentDate" class="form-control" />
<span asp-validation-for="Student.EnrollmentDate" class="text-danger"></span>
</div>
<div class="form-group">
<input type="submit" value="Create" class="btn btn-primary" />
</div>
</form>
I have debugged and analyzed the traffic. I do know that I'm receiving the form with the values and actually, by using Request.Form["student.FirstMidName"] and so on, I can populate emptyStudent.
I still don't understand what's wrong, since the code, apparently, is fine. However, TryUpdateModelAsync is returning false.
Any idea why is this happening?
ModelState. There should be errors set after TryUpdateModelAsync runs.ModelStateis a property in Controller class. Please update your Answer if this helped or if you need further assistance.public ICollection<Enrollment> Enrollments { get; } = [];Never expose a setter as with tracked entities you should also never reset a collection reference.