TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
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.
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);
}
//POST:/Home/DietaryListOptions.
[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>
Reply
Answers (
5
)
Customization of aspnet identity provider for authentication
Borders of textbox are fading away when validated?