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
items = new List (); - 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);
- }
- //POST:/Home/DietaryListOptions.
- [HttpPost]
- public ActionResult DietOptions(List
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; }
- }
- class="row">
- ="Dietary Requirements">Dietary Requirements
class="input-group col-md-offset-3 col-sm-6 col-xs-6">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
- }
Guest UserPosted Mar 23, 2020, 3:48 AM
Vinay SinghPosted Mar 23, 2020, 3:32 AM
Guest UserPosted Mar 23, 2020, 3:16 AM
Jignesh KumarPosted Mar 23, 2020, 2:51 AM
Rajanikant HawaldarPosted Mar 23, 2020, 2:19 AM