Is there an overload for Html.DropDownList that will show a value from the model as the currently selected item? In my view I insert a model using as using statement
@using SchoolIn.Models
Then I access the model like this:
if (Model.Enrollments != null)
{
@Html.DropDownList("searchString", Model.Enrollments.FirstOrDefault().weekDays.Select(s => new SelectListItem { Text = s.ToString(), Value = s.ToString() }))
}
Here’s the code from my model:
public virtual string classDays { get; set; }
public string[] weekDays = new string[6] { "Day", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday" };
public string[] WeekDays
{
get { return weekDays; }
When my view loads it presents a dropdown list that I can select from, I select a day and save the selection but when it loads again, I want the previously selected item to be the default selected in the list. How can I do this? I would really appreciate any help, thanks.
It looks like you should be able to set
Selectedon the appropriate item:You could also use
DropDownListFor, which should set the selected item to the value of the property.