I need to create a "virtual" CheckedListbox (a bunch of flags in a container), that is, N flags based on the results of a SQL Server query. I have a placeholder html where the checkboxes are currently placed on the page:
<div class="container" name="unitsCheckboxDiv"> <input type="checkbox" /> This is checkbox <br /> <input type="checkbox" /> This is checkbox <br /> <input type="checkbox" /> This is checkbox <br /> <input type="checkbox" /> This is checkbox <br /> <input type="checkbox" /> This is checkbox <br /> <input type="checkbox" /> This is checkbox <br /> <input type="checkbox" /> This is checkbox <br /> <input type="checkbox" /> This is checkbox <br /> <input type="checkbox" /> This is checkbox <br /> <input type="checkbox" /> This is checkbox <br /> </div>
... but I need to create checkboxes in response to a result set. The html above is on the page \ Views \ Home \ Index.cshtml, so I assume that the "code-behind" belongs to the file \ Controllers \ HomeController.cs, but I don’t know what to do there. I currently have standard / boilerplate code:
public class HomeController : Controller { public ActionResult Index() { return View(); } . . .
Hope I can do something like this:
Page_Load() { DataTable dt = GetUnits(); unitsCheckBoxDiv.DataSource = dt; }
... or more realistic more:
Page_Load() { DataTable dt = GetUnits(); foreach (string unit in dt) { CheckBox cb = new Checkbox(); cb.Value = unit; unitsCheckBoxDiv.AddHTMLElement(cb); } }
... but I don’t know how to make this vague idea more concrete.
UPDATE
I think I'm on the right track by implementing Prasad Raji's answer, but with this code:
HomeController.cs:
using System.Data; using System.Web.Mvc; namespace WebAppRptScheduler.Controllers { public class HomeController : Controller { public ActionResult Index() { DataTable dtable = new DataTable(); SQL.ExecuteSQLReturnDataTable(SQL.SelectUnitsQuery, CommandType.Text, null); ViewBag.Units = dtable; return View(); } . . . } }
Index.cshtml:
@using System.Data @{ ViewBag.Title = "Report Scheduler"; } <div class="jumbotron"> <h1>Report Scheduler</h1> DataTable ds = ViewBag.Units as DataTable; var rows = from x in ds.AsEnumerable() select new { unit = x.Field<string>("unit") }; </div> <div class="row"> <div class="col-md-6"> <h2>Configure One Unit/Report pair at a time</h2> <h4>Select a Unit</h4> @foreach (var item in rows) { <input id=" ckbx_@ (item.unit)" type="checkbox" value="@item.unit" /> @item.unit <br /> } <p><a class="btn btn-default" href="http://go.microsoft.com/fwlink/?LinkId=301865">Learn more »</a></p> </div> <div class="col-md-6"> <h2>Get more libraries</h2> <p>NuGet is a free Visual Studio extension that makes it easy to add, remove, and update libraries and tools in Visual Studio projects.</p> <p><a class="btn btn-default" href="http://go.microsoft.com/fwlink/?LinkId=301866">Learn more »</a></p> </div> </div>
... I get this runtime error (it compiles, although it says "Compilation error"):

UPDATE 2
Moving the code from the div and into the code block (a reasonable move):
@using System.Data @{ ViewBag.Title = "Report Scheduler"; DataTable ds = ViewBag.Units as DataTable; var rows = from x in ds.AsEnumerable() select new { unit = x.Field<string>("unit") }; }
... I can get further, but I still do not see the checkboxes on the page:

UPDATE 3
It turns out that it was a stupid add-in on my part (I would say, “rookie mistake,” but I'm not a rookie). After changing this line in the controller:
SQL.ExecuteSQLReturnDataTable(SQL.SelectUnitsQuery, CommandType.Text, null);
... to that:
dtable = SQL.ExecuteSQLReturnDataTable(SQL.SelectUnitsQuery, CommandType.Text, null);
... it works fine (there was nothing to migrate, so it is not surprising that no flags were created).