Hi, I’m trying to create a list of default values that is created in my model as an array property and then access them in a dropdown list in my view. Here's what I've been trying so far and it does not work.
namespace SchoolWorks.Models
{
public class Attendance
{
public string[] weekDays = new string[5]{"monday", "Tuesday", "Wednesday", "Thursday", "Friday"};
}
}
Then in my view something like this:
@Html.DropDownListFor(modelItem=>item.weekDays)
I’ve googled for hours without any success. Can someone please help me with this one?
now I want to access then as a DropDownList in the view. DisplayFor shows a single line of the array elements when I use this:
@Html.DisplayFor(modelItem => item.WeekDays)
but I want to use it something like this: @Html.Editor(). How can I get access to the
elements as a list that I can pick from? I'm trying to do this outside of a foreach loop.
a drop down list composes two components. the selected value, which should be in the model, and the list of possible values, which need to be a collection (IEnummerable) SelectListItems.
@Html.DropDownListFor(m => m.SelectedWeekDay, new SelectList(WeekDays))
to preselect a value, set SelectedWeekDay to a value in the list (say "Tuesday")
If your model is weekDays, it means in your action you might do like below:
In Action from one of your controller:
public ActionResult GetWeekdays()
{
string[] weekdays = new string[5] { "Monday", "Tuesday", "Wednesday", "Thursday", "Friday" }; // or from your public class propery
return View(weekdays);
}
In Your View must be like this
@model string[]
My answer for the dropdown in View is:
// Weekday will be the name of your dropdown list. You can change to anything that suite you.
@Html.DropDownList("WeekDay", new SelectList(Model))
Hopefully this answer your question.
Danny Mualim
Marked as answer by AlexanderBlade on Jun 20, 2012 04:07 AM
AlexanderBla...
Member
325 Points
309 Posts
How can I create an array in my model and then access the list in my view?
Jun 16, 2012 09:04 AM|LINK
Hi, I’m trying to create a list of default values that is created in my model as an array property and then access them in a dropdown list in my view. Here's what I've been trying so far and it does not work.
namespace SchoolWorks.Models
{
public class Attendance
{
public string[] weekDays = new string[5]{"monday", "Tuesday", "Wednesday", "Thursday", "Friday"};
}
}
Then in my view something like this:
@Html.DropDownListFor(modelItem=>item.weekDays)
I’ve googled for hours without any success. Can someone please help me with this one?
Thanks.
Mudasir.Khan
All-Star
15346 Points
3142 Posts
Re: How can I create an array in my model and then access the list in my view?
Jun 16, 2012 10:03 AM|LINK
instead of string[] please have a property and try once for example
public string[] weekDays = new string[5]{"monday", "Tuesday", "Wednesday", "Thursday", "Friday"};
public string[] WeekDays {
get {
return weekDays;
}
bkis1112
Participant
1061 Points
226 Posts
Re: How can I create an array in my model and then access the list in my view?
Jun 16, 2012 02:34 PM|LINK
In your controller:
List<SelectListItem> listDay = new List<SelectListItem>(); listDay.Add(new SelectListItem { Text = "-Choose-", Value = "Selects items" }); listDay.Add(new SelectListItem { Text = "Monday", Value = "Monday" }); listDay.Add(new SelectListItem { Text = "Tuesday", Value = "Tuesday" }); listDay.Add(new SelectListItem { Text = "Thursday", Value = "Thursday" }); listDay.Add(new SelectListItem { Text = "Wenesday", Value = "Wenesday" }); listDay.Add(new SelectListItem { Text = "Friday", Value = "Friday" }); listDay.Add(new SelectListItem { Text = "Saturday", Value = "Saturday" }); listDay.Add(new SelectListItem { Text = "Sunday", Value = "Sunday" }); ViewData["day"] = listDay;In your View:
@Html.DropDownList("Id", (IEnumerable<SelectListItem>)ViewData["day"], new { id = "Id" })thanks for reading my post
Trần Lê Thành Trung
AlexanderBla...
Member
325 Points
309 Posts
Re: How can I create an array in my model and then access the list in my view?
Jun 16, 2012 07:26 PM|LINK
Thanks, this seem to work:
public string[] weekDays = new string[5] { "monday", "Tuesday", "Wednesday", "Thursday", "Friday" };
public string[] WeekDays
{
get { return weekDays; }
}
now I want to access then as a DropDownList in the view. DisplayFor shows a single line of the array elements when I use this:
@Html.DisplayFor(modelItem => item.WeekDays)
but I want to use it something like this: @Html.Editor(). How can I get access to the elements as a list that I can pick from? I'm trying to do this outside of a foreach loop.
Thanks for any help.
bruce (sqlwo...
All-Star
37626 Points
5574 Posts
Re: How can I create an array in my model and then access the list in my view?
Jun 17, 2012 02:21 AM|LINK
a drop down list composes two components. the selected value, which should be in the model, and the list of possible values, which need to be a collection (IEnummerable) SelectListItems.
@Html.DropDownListFor(m => m.SelectedWeekDay, new SelectList(WeekDays))
to preselect a value, set SelectedWeekDay to a value in the list (say "Tuesday")
Unnics
Member
394 Points
93 Posts
Re: How can I create an array in my model and then access the list in my view?
Jun 18, 2012 04:23 PM|LINK
Hi try this.
Add the below code in the controller
Add the below code in the view
@html.dropdownlist("weekdays",(SelectList)ViewBag.weeks,"--select--")Hope this will help.
DannyMualim
Member
260 Points
77 Posts
Re: How can I create an array in my model and then access the list in my view?
Jun 18, 2012 11:54 PM|LINK
If your model is weekDays, it means in your action you might do like below:
In Action from one of your controller:
public ActionResult GetWeekdays() { string[] weekdays = new string[5] { "Monday", "Tuesday", "Wednesday", "Thursday", "Friday" }; // or from your public class propery return View(weekdays); }In Your View must be like this
My answer for the dropdown in View is:
// Weekday will be the name of your dropdown list. You can change to anything that suite you. @Html.DropDownList("WeekDay", new SelectList(Model))Hopefully this answer your question.
Danny Mualim
AlexanderBla...
Member
325 Points
309 Posts
Re: How can I create an array in my model and then access the list in my view?
Jun 20, 2012 04:07 AM|LINK
Thank you so much!!