I have a view that displays a list of records in a table. I want to be able to select different AttendanceStatusId values via a SelectList drop-down, one per table record. The following code updates the AttendanceStatusId value if I hardcode a value, such as
episodeSanction
.GetType()
.GetProperty("AttendanceStatusId")?
.SetValue(episodeSanction, new Guid("01920309-1939-2930-93482930"));
But if I try:
episodeSanction
.GetType()
.GetProperty("AttendanceStatusId")?
.SetValue(episodeSanction, episodeSanction.AttendanceStatusId);
it just uses the existing value, meaning no updated value.
if (!await Update(vm))
{
SetNotification("Error updating the database.");
vm.AttendanceList = await SelectListBuilder.GetList("AttendanceStatus");
return View($"{ViewBase}/Attendance.cshtml", vm);
}
public async Task<bool> Update(SanctionViewModel vm)
{
var model = await _db.Sanctions.FindAsync(vm.Sanction.Id);
await _db.Entry(model).Collection(x => x.EpisodeSanctions).LoadAsync();
foreach (var episodeSanction in model.EpisodeSanctions)
{
episodeSanction
.GetType()
.GetProperty("AttendanceStatusId")?
.SetValue(episodeSanction, episodeSanction.AttendanceStatusId);
}
return await UpdateDatabaseAsync(_db);
}
<td class="text-center">
<div class="form-group">
<select class="form-control" id="Sanction.EpisodeSanctions_[@i]_AttendanceStatusId" name="Sanction.EpisodeSanctions[@i].AttendanceStatusId">
<option value=""></option>
@foreach (var attendance in Model.AttendanceList)
{
<option [email protected] @if (attendance.Value == attendanceStatus) { <text> selected="selected" </text> }>
@attendance.Text
</option>
}
</select>
@Html.FormValidationMessageFor(m => m.Sanction.EpisodeSanctions.ToArray()[i].AttendanceStatusId)
</div>
</td>