4

I'm trying to work around the lack of a CheckBoxList in ASP.NET MVC. I've gotten to the point I can render a list of Enum values just fine, but I'm stuck on how to set the checked attribute based on my Model - Which in this case is a User entity that has an IList of Role entities. The role id's correspond to the enum values.

This example is using the Spark View Engine syntax, but it's functionally identical to the standard ASP.NET MVC view engine ("$(" is the same as "<%=" or "<%")

<for each="var r in Enum.GetValues(typeof(UserRole))">
    <label><input type="checkbox" name="Roles" value="${(int)r}" checked="[How-The-Heck-To-I-Get-This?]" />${r}</label>
</for>
1
  • I might be missing something here, but can't you use the Contains method on your Role list? model.Roles.Contains(r) ? = 'checked' : '' Commented Sep 23, 2009 at 21:16

4 Answers 4

9

If your roles are defined like this then you can associate more than one role with the user

[Flags]
public enum UserRole
{        
    DataReader = 1,
    ProjectManager = 2,
    Admin = 4,
}

By adding a simple extension method you can check if you role contains a target role

public static class RoleExtension
{
    public static bool HasRole(this UserRole targetVal, UserRole checkVal)
    {
        return ((targetVal & checkVal) == checkVal);
    }
}

Use the extension method in you view to update the check box, not sure if the following is the correct way for your view engine.

<for each="var r in Enum.GetValues(typeof(UserRole))">
<label>
    <input 
       type="checkbox"
       name="Roles" 
       value="${(int)r}" 
       checked="${Model.Role.HasRole(r) ? "checked" : string.Empty}" />
</label>

Sign up to request clarification or add additional context in comments.

Comments

3
<for each="var r in Enum.GetValues(typeof(UserRole))">
    <label>
      <% if (r.Checked) { %>
        <input type="checkbox" checked="checked" />${r}
      <% } else { %>
        <input type="checkbox" />${r}
      <% } %>
    </label>
</for>

P.S. The added syntax is for the standard view engine, may not compile but the idea should be clear.

Comments

2

Check equality with the model role and set checked to "checked" when equal and string.Empty when not.

<label>
    <input type="checkbox"
           name="Roles"
           value="${(int)r}"
           ${ Model.Role == r ? "checked='checked'" : string.Empty } />
    ${r}
</label>

Comments

1

Hey, I actually haven't been able to get the above methods to work. Setting the value of a "checked" attribute to an empty string still results in a checked checkbox in IE. My solution was to add an HtmlHelper extension:

public static string SimpleCheckbox(this HtmlHelper helper, 
                                    string name, 
                                    string value, 
                                    bool isChecked)
{
    return String.Format("<input type=\"checkbox\" name=\"{0}\" value=\"{1}\" " + (isChecked ? "checked" : "") + "/>", name, value);
}

And in the markup:

<%= Html.SimpleCheckbox("checkboxId", item.Id, item.IsSelected) %>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.