ITWorker ITWorker - 2 months ago 6
C# Question

How to generate checkboxes from string list in viewbag?

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.

Answer

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!

Comments