Guest User

Guest User

  • Tech Writer
  • 2.1k
  • 473.4k

How to call more than two models within your View?

Mar 23 2020 1:59 AM
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.
 
  1.   public ActionResult DietOptions()  
  2.        {  
  3.            List<SelectListItem> items = new List<SelectListItem>();  
  4.            string constr = ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString;  
  5.            using(SqlConnection con = new SqlConnection(constr))  
  6.            {  
  7.                string query = "SELECT Vegetarian, None FROM Dietary";  
  8.                using(SqlCommand cmd = new SqlCommand(query))  
  9.                {  
  10.                    cmd.Connection = con;  
  11.                    con.Open();  
  12.                    using(SqlDataReader sdr = cmd.ExecuteReader())  
  13.                    {  
  14.                        while(sdr.Read())  
  15.                        {  
  16.                            items.Add(new SelectListItem  
  17.                            {  
  18.                                Text = sdr["Vegetarian"].ToString(),  
  19.                                Value = sdr["None"].ToString()  
  20.                            });  
  21.   
  22.                        }  
  23.                    }  
  24.                    con.Close();  
  25.                }  
  26.            }  
  27.            return View(items);  
  28.        }  
  29.   
  30.   
  31.        //POST:/Home/DietaryListOptions.  
  32.        [HttpPost]  
  33.        public ActionResult DietOptions(List<SelectListItem> items)  
  34.        {  
  35.            ViewBag.Message = "Selected Items:\\n";  
  36.            foreach(SelectListItem item in items)  
  37.            {  
  38.                if(item.Selected)  
  39.                {  
  40.                    ViewBag.Message += string.Format("{0}\\n", item.Text);  
  41.                }  
  42.            }  
  43.            return View(items);  
  44.        }  
  45.   
  46.  public class RoleViewModel  
  47.    {  
  48.        public string UserId { getset; }  
  49.        public string Username { getset; }  
  50.        public string Useremail { getset; }  
  51.        public string RoleId { getset; }  
  52.   
  53.        public EditTrainingRegFormViewModel HomeMainModel { getset; }  
  54.   
  55.        public CheckBoxViewModel DietaryModel { getset; }  
  56.    }  
  57. public class CheckBoxViewModel  
  58.    {  
  59.          
  60.        public string None { getset; }  
  61.        public string Vegetarian { getset; }  
  62.   
  63.        public string Vegan { getset; }  
  64.        public string  Halaal { getset; }  
  65.        public string Other { getset; }  
  66.   
  67.          
  68.    }  
  69.   
  70. <div class="row">  
  71.                                <label for="Dietary Requirements">Dietary Requirements</label>  
  72.                                <div class="input-group col-md-offset-3 col-sm-6 col-xs-6">  
  73.                                    <div class="input-group pull-left">  
  74.                                                      
  75.                                        @using(Html.BeginForm("DietOptions""Home", FormMethod.Post))  
  76.                                        {  
  77.                                           @Html.CheckBoxFor(m=>m.DietaryModel.Vegetarian, new {@class = "form-control"})  // How make this access those item list? I am struggling here on my View
  78.                                        }  
  79.   
  80.                                    </div>  
  81.                                      
  82.                                </div>  
  83.                                 
  84.                            </div>  
 

Answers (5)