我正在寻找一种更好的方法,在MVC视图中包含的表单中使用RadioButtons来显示数据。 我目前正在使用的一个示例是显示MembershipUser所属的角色。
我正在做的是设置字符串变量的值以在我的输入标签中输出,这将在必要时将RadioButton设置为已选中。正如您从下面的代码中看到的,这是一个非常难看的黑客。
您能否建议更好的方式来展示这些数据?
<%
string User = string.Empty;
if (Roles.IsUserInRole(Model.UserName, "User"))
{
User = "checked='checked'";
}
string Priviliged = string.Empty;
if (Roles.IsUserInRole(Model.UserName, "Privileged"))
{
Priviliged = "checked='checked'";
User = string.Empty;
}
string Admin = string.Empty;
if (Roles.IsUserInRole(Model.UserName, "Admin"))
{
Admin = "checked='checked'";
Priviliged = string.Empty;
}
string SuperAdmin = string.Empty;
if (Roles.IsUserInRole(Model.UserName, "SuperAdmin"))
{
SuperAdmin = "checked='checked'";
Admin = string.Empty;
}
%>
<input type="radio" name="Permission" id="Permission" value="SuperAdmin" <%=SuperAdmin %> />SuperAdmin
<input type="radio" name="Permission" id="Permission" value="Admin" <%=Admin %> />Admin
<input type="radio" name="Permission" id="Permission" value="Privileged" <%=Priviliged %> />Privileged
<input type="radio" name="Permission" id="Permission" value="User" <%=User %> />User
答案 0 :(得分:2)
也许您可以尝试使用HTML帮助程序,例如:
<%=Html.RadioButton("Permission", "SuperAdmin", Roles.IsUserInRole(Model.UserName, "SuperAdmin"))%>
<%=Html.RadioButton("Permission", "Admin", Roles.IsUserInRole(Model.UserName, "Admin"))%>
<%=Html.RadioButton("Permission", "Privileged", Roles.IsUserInRole(Model.UserName, "Privileged"))%>
<%=Html.RadioButton("Permission", "User", Roles.IsUserInRole(Model.UserName, "User"))%>
答案 1 :(得分:1)
我更喜欢&lt; select /&gt;标签包含多行并启用了多选,例如
<select id="permissions" name="permissions" size="4" multiple="multiple">
<option value="SuperAdmin">Super Admin</option
....
</select>