lundi 3 octobre 2016

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" attribute 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?

Thank you.

Aucun commentaire:

Enregistrer un commentaire