I'm looking for a better way to use RadioButtons in a form contained inside an MVC view to display data. One example I'm currently working with is displaying the Role in which a MembershipUser belongs.
What I'm doing is setting values on string variables to output in my input tags that will set the RadioButton as checked where necessary. As you can see from the code below, this is an extremely ugly hack.
Can you suggest a better way to present this data?
<%
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