1

I have the following code in my controller method that returns the view:

public ActionResult Create()
{
  var allprivs = new SQLRolerecord().GetAllPrivsInApp();
  ViewBag.AllPrivsInApp = allprivs;
  return View();
}

where the GetAllPrivsInApp method returns an IEnumerable<string>. Then in the view, I have the following Razor/html in my attempt to make a list of checkboxes from the returned string list:

<table>
@{
  List<string> privsInApp = ViewBag.AllPrivsInApp;
  foreach (var priv in privsInApp)
  {
    @Html.CheckBox(priv,  new { value = priv, @label=priv})
  }
}
</table>

This generates the correct number of checkboxes I expect, however, I don't see a label next to them. When viewing the HTML source when the application is running, I can see that the name and several other attributes for each of the checkboxes contains the text value I expect for each of the textboxes. How can I get the text to show up next to each of the checkboxes?

Here is a screenshot of what shows up:

enter image description here

And here is the source that was generated:

<table>
<input id="AdminRead" label="AdminRead" name="AdminRead" type="checkbox" value="AdminRead" /><input name="AdminRead" type="hidden" value="false" /><input id="AdminWrite" label="AdminWrite" name="AdminWrite" type="checkbox" value="AdminWrite" /><input name="AdminWrite" type="hidden" value="false" /><input id="Privilege1" label="Privilege1" name="Privilege1" type="checkbox" value="Privilege1" /><input name="Privilege1" type="hidden" value="false" /><input id="Privilege2" label="Privilege2" name="Privilege2" type="checkbox" value="Privilege2" /><input name="Privilege2" type="hidden" value="false" /><input id="Privilege3" label="Privilege3" name="Privilege3" type="checkbox" value="Privilege3" /><input name="Privilege3" type="hidden" value="false" /><input id="Privilege4" label="Privilege4" name="Privilege4" type="checkbox" value="Privilege4" /><input name="Privilege4" type="hidden" value="false" /><input id="Privilege5" label="Privilege5" name="Privilege5" type="checkbox" value="Privilege5" /><input name="Privilege5" type="hidden" value="false" /><input id="Privilege6" label="Privilege6" name="Privilege6" type="checkbox" value="Privilege6" /><input name="Privilege6" type="hidden" value="false" /><input id="Privilege7" label="Privilege7" name="Privilege7" type="checkbox" value="Privilege7" /><input name="Privilege7" type="hidden" value="false" /><input id="Privilege8" label="Privilege8" name="Privilege8" type="checkbox" value="Privilege8" /><input name="Privilege8" type="hidden" value="false" /><input id="Privilege9" label="Privilege9" name="Privilege9" type="checkbox" value="Privilege9" /><input name="Privilege9" type="hidden" value="false" /><input id="Privilege0" label="Privilege0" name="Privilege0" type="checkbox" value="Privilege0" /><input name="Privilege0" type="hidden" value="false" />
</table>

Thank you.

ITWorker
  • 965
  • 2
  • 16
  • 39
  • you will nee to do @Html.CheckBoxFor(priv); @priv; there is noit @label for checkbox – Emil Oct 03 '16 at 14:05
  • @Emil when I changed it to `@Html.CheckBoxFor(priv);` it gives me an error regarding converting the argument `priv` to a supported type needed. – ITWorker Oct 03 '16 at 14:09
  • 2
    sorry, do this @Html.CheckBox(priv, new { value = priv}); @priv;. Checkbox has no "label " – Emil Oct 03 '16 at 14:11
  • @Emil thanks that worked! – ITWorker Oct 03 '16 at 14:12
  • 1
    If you are just looking for a label, why don't you just do `@Html.Label(priv)` above your `@Html.CheckBox(priv, new { value = priv})`? Of course this won't style it correctly, so you can edit that as you please – Grizzly Oct 03 '16 at 14:15
  • @BviLLe_Kid this works too, I just didn't know such a helper exists. – ITWorker Oct 03 '16 at 14:18
  • @ITWorker I see. I added my solution down below to give more of a visual. – Grizzly Oct 03 '16 at 14:22
  • 1
    Do not use the `@CheckBox()` method which is for binding to a `bool` property, and you cannot get true 2-way model binding with your implementation. Use a view model (refer [this answer](http://stackoverflow.com/questions/29542107/pass-list-of-checkboxes-into-view-and-pull-out-ienumerable/29554416#29554416) for an example). Otherwise you may as well just use `` which will post the checked values back to a parameter `string[] xxx` –  Oct 03 '16 at 22:12

1 Answers1

1

If you are solely looking for a label for the checkbox, then this should work:

<table>
@{
  List<string> privsInApp = ViewBag.AllPrivsInApp;
  foreach (var priv in privsInApp)
  {
        <tr>
            <td>@Html.Label(priv)</td>
            <td>@Html.CheckBox(priv, new { value = priv })</td>
        </tr>
  }
}
</table>

This is also assuming that you are using Bootstrap for styling.

Let me know if that helps!

Grizzly
  • 5,873
  • 8
  • 56
  • 109