In Asp.net MVC 3, I have my radio buttons set up like this:
<div class="editor-label">
@Html.LabelFor(m => m.Roles)
</div>
<div class="editor-field">
@Html.RadioButtonFor(model => model.Roles,false) SuperAdmin
@Html.RadioButtonFor(model => model.Roles,false) Admin
@Html.ValidationMessageFor(model =>model.Roles)<br/>
</div>
Which works fine, but this requires me to hardcode the values. Right now I only have two values, but it could change based on data in my db. How could I populate this list of radiobuttons dynamically from the database? Thanks
I have a Register ActionResult method in my Controller like this:
public ActionResult Register()
{
// On load, query AdminRoles table and load all admin roles into the collection based on the
// the query which just does an order by
AdminRolesQuery arq = new AdminRolesQuery();
AdminRolesCollection myAdminRolesColl = new AdminRolesCollection();
arq.OrderBy(arq.RoleName, EntitySpaces.DynamicQuery.esOrderByDirection.Ascending);
myAdminRolesColl.Load(arq);
// Store The list of AdminRoles in the ViewBag
//ViewBag.RadioAdminRoles = myAdminRolesColl.Select(r => r.RoleName).ToList();
ViewData.Model = myAdminRolesColl.Select(r => r.RoleName).ToList();
return View();
}
So the ViewData.Model has a list of AdminRoles. How would I then access this list in my model?