[英]How can I have the values from my model match the selected values in my dropdown lists?
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. 我的视图类型为:@model SchoolIn.Models.Student,但我想插入另一个模型Model.Enrollments并最小化下拉列表项。
<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>
@Html.DropDownListFor( x => x.courseId, new SelectList(LookupUtils.AvailableCourcesList(), "Value", "Text", Model.courseId))
LookupUtils is a static class have: LookupUtils是一个静态类,具有:
public static List<SelectListItem> AvailableCourcesList()
{
var dataContext = new YourDataContext( );
var data = dataContext.GetCourcesFn().ToList();
var result = ( from res in data
select new SelectListItem()
{
Text = res.courseName,
Value = res.courseId.ToString()
} ).ToList();
return result;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.