【发布时间】:2011-10-19 20:55:38
【问题描述】:
在 Asp.net MVC 3 中,我的单选按钮设置如下:
<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>
这很好用,但这需要我对值进行硬编码。现在我只有两个值,但它可能会根据我的数据库中的数据而改变。如何从数据库中动态填充此单选按钮列表?谢谢
我的控制器中有一个注册 ActionResult 方法,如下所示:
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();
}
所以 ViewData.Model 有一个 AdminRoles 列表。然后我将如何在我的模型中访问此列表?
【问题讨论】:
-
Model.Roles 是任何角色项的集合吗?
-
签出此link
标签: c# asp.net asp.net-mvc-3