I've seen the other threads on the subject, but am having trouble with what I believe is the correct syntax/setup. Ultimately the controller is not able to see the contents of the Role list when it is being returned from an edit.
View
@model Models.Volunteer
@foreach (var item in Model.Roles)
{
<input type="checkbox" asp-for="@item.Selected" />
<label asp-for="@item.Selected">@item.RoleName</label>
<input type="hidden" asp-for="@item.RoleId" />
<input type="hidden" asp-for="@item.RoleName" />
<br />
}
<input type="submit" value="Save" class="btn btn-default" />
Model
public abstract class BaseVolunteer
{
[Key]
public int Recno { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string Email { get; set; }
public string City { get; set; }
[NotMapped]
[Display(Name = "Roles")]
public List<Role> Roles { get; set; }
}
public class Role
{
[Key]
public String RoleId { get; set; }
public String RoleName { get; set; }
public bool Selected { get; set; }
}
Controller
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Edit(int id, [Bind("Recno,FirstName,LastName,Email,City,Roles")] Volunteer volunteer)
{
}
The volunteer object in the controller correctly returns the other values... but the Roles object is NULL despite being properly populated for display.