C
C#2y ago
Wrenpo

✅ MVC Core: Model Properties Not Passing to Controller Method

I have a model that I'm trying to have passed back to a controller method, but its properties are appearing as null. The correct POST method is being hit. I have listed out each property in the form to POST back via Html.HiddenFor(x => x.PropertyName) as I have done in many places within this project. They are still coming back null. Any ideas as to what may be going on?
3 Replies
Angius
Angius2y ago
Show your controller Usually, the issue has something to do with async. Either something not being awaited or some async void method somewhere
Wrenpo
Wrenpo2y ago
[HttpPost]
[Authorize]
public async Task<IActionResult> TrainingSessionRosterReview(int id, TrainingSessionRosterReview model, CancellationToken cancellationToken = default)
{
List<CourseSessionRegistration> courseSessionRegistrations = await this.DbContext.CourseSessionRegistrations.Where(x => x.CourseSessionId == id).ToListAsync(cancellationToken);

foreach (CourseSessionRegistration courseSessionRegistration in courseSessionRegistrations)
{
TrainingSessionRosterRegistrant? currentRegistrant = model.Registrants.FirstOrDefault(reg => reg.RegistrantId == courseSessionRegistration.RegistrantId);

if (currentRegistrant != null)
{
courseSessionRegistration.Attended = currentRegistrant.IsAttendanceConfirmed;
courseSessionRegistration.AttendanceConfirmationDate = currentRegistrant.IsAttendanceConfirmed ? DateTime.Now : null;
}
}

await this.DbContext.SaveChangesAsync(cancellationToken);

return this.RedirectToAction(nameof(AdminController.TrainingSessionRoster));
}
[HttpPost]
[Authorize]
public async Task<IActionResult> TrainingSessionRosterReview(int id, TrainingSessionRosterReview model, CancellationToken cancellationToken = default)
{
List<CourseSessionRegistration> courseSessionRegistrations = await this.DbContext.CourseSessionRegistrations.Where(x => x.CourseSessionId == id).ToListAsync(cancellationToken);

foreach (CourseSessionRegistration courseSessionRegistration in courseSessionRegistrations)
{
TrainingSessionRosterRegistrant? currentRegistrant = model.Registrants.FirstOrDefault(reg => reg.RegistrantId == courseSessionRegistration.RegistrantId);

if (currentRegistrant != null)
{
courseSessionRegistration.Attended = currentRegistrant.IsAttendanceConfirmed;
courseSessionRegistration.AttendanceConfirmationDate = currentRegistrant.IsAttendanceConfirmed ? DateTime.Now : null;
}
}

await this.DbContext.SaveChangesAsync(cancellationToken);

return this.RedirectToAction(nameof(AdminController.TrainingSessionRoster));
}
Disregard. I've figured out several issues. The main one being an AutoMapper issue.
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server