Hi Team
I have two models within a view and have created a checkbox, inside i cant seem to call all of my item list. Who can help with this logic below of mine? What i want here to get all lists of my items from table column within database.
- public ActionResult DietOptions()
- {
- List<SelectListItem> items = new List<SelectListItem>();
- string constr = ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString;
- using(SqlConnection con = new SqlConnection(constr))
- {
- string query = "SELECT Vegetarian, None FROM Dietary";
- using(SqlCommand cmd = new SqlCommand(query))
- {
- cmd.Connection = con;
- con.Open();
- using(SqlDataReader sdr = cmd.ExecuteReader())
- {
- while(sdr.Read())
- {
- items.Add(new SelectListItem
- {
- Text = sdr["Vegetarian"].ToString(),
- Value = sdr["None"].ToString()
- });
-
- }
- }
- con.Close();
- }
- }
- return View(items);
- }
-
-
-
- [HttpPost]
- public ActionResult DietOptions(List<SelectListItem> items)
- {
- ViewBag.Message = "Selected Items:\\n";
- foreach(SelectListItem item in items)
- {
- if(item.Selected)
- {
- ViewBag.Message += string.Format("{0}\\n", item.Text);
- }
- }
- return View(items);
- }
-
- public class RoleViewModel
- {
- public string UserId { get; set; }
- public string Username { get; set; }
- public string Useremail { get; set; }
- public string RoleId { get; set; }
-
- public EditTrainingRegFormViewModel HomeMainModel { get; set; }
-
- public CheckBoxViewModel DietaryModel { get; set; }
- }
- public class CheckBoxViewModel
- {
-
- public string None { get; set; }
- public string Vegetarian { get; set; }
-
- public string Vegan { get; set; }
- public string Halaal { get; set; }
- public string Other { get; set; }
-
-
- }
-
- <div class="row">
- <label for="Dietary Requirements">Dietary Requirements</label>
- <div class="input-group col-md-offset-3 col-sm-6 col-xs-6">
- <div class="input-group pull-left">
-
- @using(Html.BeginForm("DietOptions", "Home", FormMethod.Post))
- {
- @Html.CheckBoxFor(m=>m.DietaryModel.Vegetarian, new {@class = "form-control"}) // How make this access those item list? I am struggling here on my View
- }
-
- </div>
-
- </div>
-
- </div>