views:

214

answers:

2

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
+1  A: 

I'd prefer a <select /> tag with multiple rows and multi-select enabled, e.g.

<select id="permissions" name="permissions" size="4" multiple="multiple">
   <option value="SuperAdmin">Super Admin</option
   ....
</select>
philjohn
That actually is what I'm using in the application. However, this radiobutton question has been bothering me for some time and I'm looking for an elegant solution
splatto
I should also add that you probably want checkboxes rather than radio buttons, unless there is never going to be an overlap of roles (i.e. philjohn isa user, and philjohn isa admin).
philjohn
The idea was initially that if you were one role, you were also all roles below it. But I think I will be changing that based on the answers to this question.
splatto
+1  A: 

Maybe you can try using the HMTL helper, something like:

<%=Html.RadioButton("Permission", "SuperAdmin", Roles.IsUserInRole(Model.UserName, "SuperAdmin") ? true : false)%>
<%=Html.RadioButton("Permission", "Admin", Roles.IsUserInRole(Model.UserName, "Admin") ? true : false)%>
<%=Html.RadioButton("Permission", "Privileged", Roles.IsUserInRole(Model.UserName, "Privileged") ? true : false)%>
<%=Html.RadioButton("Permission", "User", Roles.IsUserInRole(Model.UserName, "User") ? true : false)%>
Omar
Thanks Omar. Initially I wanted that if a user was in a specific role, they were also in every role beneath it, but I will be changing that implementation. Your answer is perfect.
splatto
Yep, like philjohn suggested (+1), its more readable if checkboxes are used, specially when the user is in multiple roles.
Omar