How can I have the values from my model match the selected values in my dropdown lists?
What I would like to do is to have the dropdown list reflect the property value of the model. I've only been able to show the same value in all of the dropdown lists. I've been trying to do this:
@Html.DropDownList("searchString", Model.Enrollments.FirstOrDefault().WeekDays.Select(s =>
new SelectListItem { Text = s.ToString(), Value = s.ToString(),
Selected = s.ToString().Equals(Model.Enrollments.FirstOrDefault().classDays) }))
but with no luck.
A snippet of my Enrollment model:
public string[] weekDays = new string[6] { "Day", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday" };
public string[] WeekDays
{
get { return weekDays; }
}
My view is of type: @model SchoolIn.Models.Student but I would like to interject another model, Model.Enrollments and minipulate the dropdown list items.
<table>
<tr>
@{
int cnt = 0;
List<SchoolIn.ViewModels.EnrolledCourseData> courses = ViewBag.Courses;
foreach (var course in courses)
{
if (cnt++ % 4 == 0)
{
@: </tr> <tr>
}
@: <td >
<br /><br /><br />
<input type="checkbox"
name="selectedCourses"
value="@course.CourseID"
@(Html.Raw(course.Assigned ? "checked=\"checked\"" : "")))
/>
@*course.CourseID @: @course.Title*@
@course.Title<br /> <br />
if (Model.Enrollments != null)
{
@Html.DropDownList("searchString", Model.Enrollments.FirstOrDefault().WeekDays.Select(s => new SelectListItem { Text = s.ToString(), Value = s.ToString(), Selected = s.ToString().Equals(Model.Enrollments.FirstOrDefault().classDays) }))
}
@:</td>
}
@:</tr>
}
</table>