1

I have user roles defined in my MVC application. Essentially, what I want is this:

if (User.IsInRole = ("staff"))
   {
      // disable all checkboxes
   }

I know I could do something like this, but there's about 100 checkboxes on the page and it seems obnoxious to repeat all the lines of code with the disabled attribute added. Is there a better way? I am not opposed to using some jQuery to accomplish this either:

if (User.IsInRole = ("staff"))
{
   <tr>
     <td>Centroid</td>
     <td><input type="checkbox" name="Staff" checked disabled /></td>
     <td>@Html.CheckBoxFor(m => m.NBTC_FA_Centroid, new {@disabled = "disabled")</td>
     <td>@Html.CheckBoxFor(m => m.Contract_FA_Centroid, new {@disabled = "disabled")</td>
     <td>@Html.CheckBoxFor(m => m.Coord_FA_Centroid, new {@disabled = "disabled")</td>
     <td>@Html.CheckBoxFor(m => m.NGO_FA_Centroid, new {@disabled = "disabled")</td>
     <td>@Html.CheckBoxFor(m => m.Public_FA_Centroid, new {@disabled = "disabled")</td>
   </tr>
}
else
{
   <tr>
        <td>Centroid</td>
        <td><input type="checkbox" name="Staff" checked disabled /></td>
        <td>@Html.CheckBoxFor(m => m.NBTC_FA_Centroid)</td>
        <td>@Html.CheckBoxFor(m => m.Contract_FA_Centroid)</td>
        <td>@Html.CheckBoxFor(m => m.Coord_FA_Centroid)</td>
        <td>@Html.CheckBoxFor(m => m.NGO_FA_Centroid)</td>
        <td>@Html.CheckBoxFor(m => m.Public_FA_Centroid)</td>
   </tr>
}
MKF
  • 426
  • 7
  • 24

2 Answers2

4

You can check your model and set object as disabled as attribute of html helper.

object attributes = null;

if (User.IsInRole = ("staff"))
{
    attributes = new { disabled = "disabled" };
}

and then use it to your helper like this

 @Html.CheckBoxFor(model => model.Status, attributes)

It will disable checkboxes when role is stuff otherwise it will remain enable.

  • 1
    Would you mind explaining the use of `model.Status` (or point me to some documentation)? I'm not sure what you mean by "check your model" - do you mean check the model state? – MKF Oct 02 '18 at 16:50
  • 1
    It's my property name. You should use your ones. for example: `@Html.CheckBoxFor(m => m.NBTC_FA_Centroid, attributes)` so on @MKF – Ziaul Kabir Fahad Oct 02 '18 at 16:53
  • 1
    Basically It's get ride of redundant if else clause for 100 checkboxes as you mentioned. @MKF – Ziaul Kabir Fahad Oct 02 '18 at 16:56
  • 1
    Ahh I see. I thought `Status` was something special. Thank you! – MKF Oct 02 '18 at 16:59
  • Would there be a way then to re-enable a specific checkbox easily (say checkbox 27) while leaving everything else disabled? – JosephDoggie Oct 02 '18 at 18:27
  • 1
    In such case I prepare Jquery solutions. See [link](https://stackoverflow.com/questions/2330209/jquery-checkbox-enable-disable) @JosephDoggie – Ziaul Kabir Fahad Oct 02 '18 at 18:52
1

Try this

<script type="text/javascript">
    function Uncheckall() {

        $('table input[type=checkbox]').attr('disabled', 'true');
    }
</script>
China Syndrome
  • 953
  • 12
  • 24