Note that there are some explanatory texts on larger screens.

plurals
  1. POHow can I have the values from my model match the selected values in my dropdown lists?
    text
    copied!<p>How can I have the values from my model match the selected values in my dropdown lists?</p> <p>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: </p> <pre><code>@Html.DropDownList("searchString", Model.Enrollments.FirstOrDefault().WeekDays.Select(s =&gt; new SelectListItem { Text = s.ToString(), Value = s.ToString(), Selected = s.ToString().Equals(Model.Enrollments.FirstOrDefault().classDays) })) </code></pre> <p>but with no luck.</p> <p>A snippet of my Enrollment model:</p> <pre><code> public string[] weekDays = new string[6] { "Day", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday" }; public string[] WeekDays { get { return weekDays; } } </code></pre> <p>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.</p> <pre><code> &lt;table&gt; &lt;tr&gt; @{ int cnt = 0; List&lt;SchoolIn.ViewModels.EnrolledCourseData&gt; courses = ViewBag.Courses; foreach (var course in courses) { if (cnt++ % 4 == 0) { @: &lt;/tr&gt; &lt;tr&gt; } @: &lt;td &gt; &lt;br /&gt;&lt;br /&gt;&lt;br /&gt; &lt;input type="checkbox" name="selectedCourses" value="@course.CourseID" @(Html.Raw(course.Assigned ? "checked=\"checked\"" : ""))) /&gt; @*course.CourseID @: @course.Title*@ @course.Title&lt;br /&gt; &lt;br /&gt; if (Model.Enrollments != null) { @Html.DropDownList("searchString", Model.Enrollments.FirstOrDefault().WeekDays.Select(s =&gt; new SelectListItem { Text = s.ToString(), Value = s.ToString(), Selected = s.ToString().Equals(Model.Enrollments.FirstOrDefault().classDays) })) } @:&lt;/td&gt; } @:&lt;/tr&gt; } &lt;/table&gt; </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload