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
- }
Rajanikant HawaldarRank #110 · 20.3K reputationPosted Mar 22, 2020, 10:19 PM EDT
Jignesh KumarRank #91 · 22.9K reputationPosted Mar 22, 2020, 10:51 PM EDT
Guest UserRank #877 · 2.5K reputationPosted Mar 22, 2020, 11:16 PM EDT
Vinay SinghRank #604 · 4K reputationPosted Mar 22, 2020, 11:32 PM EDT
Guest UserRank #877 · 2.5K reputationPosted Mar 22, 2020, 11:48 PM EDT
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questionsTop experts