我有以下代码,允许教师查看可用课程的下拉列表,按名称列出。当老师选择下拉选项时,我希望视图上的表单自动填充代表所选课程的默认值。填充字段的最有效方法是什么?
注意:在下拉列表中选择“自定义”时,我希望下拉列表下方显示的表单只有空格。
课程控制器
// GET: /Course/ApplyToTeach
public ActionResult ApplyToTeach()
{
var course = db.CourseTemplates;
var model = new ApplyCourseViewModel
{
Courses = course.Select(x => new SelectListItem
{
Value = x.Title,
Text = x.Title,
})
};
return View(model);
}
ApplyToTeachViewModel
public class ApplyToTeachViewModel
{
[Display(Name = "selected course")]
public string SelectedCourse { get; set; }
public IEnumerable<SelectListItem> Courses { get; set; }
}
ApplyToTeach(查看)- 请注意,我目前在这里只有下拉菜单,我正在寻找在此下拉菜单下方添加自动填充表单的最有效方法。
<h2>ApplyToTeach</h2>
@using (Html.BeginForm())
{
@Html.ValidationSummary(true)
<fieldset>
<legend>Apply To Teach</legend>
<div class="editor-label">
Which class would you like to teach? (select "Custom" if you would like to submit a customized class)
</div>
<div class="editor-field">
@Html.DropDownListFor(x => x.SelectedCourse, Model.Courses, "Custom")
@Html.ValidationMessageFor(model => model.Courses)
</div>
<p>
<input type="submit" value="Submit" />
</p>
</fieldset>
}
下拉字段的数据来自以下模型 - CourseTemplates
public class CourseTemplates
{
public int CourseTemplatesID { get; set; }
public string Title { get; set; }
public int Credits { get; set; }
public int AttendingDays { get; set; } // 10, 8, 3, or custom
public int AttendanceCap { get; set; } // default of 30
public string Location { get; set; }
public string Description { get; set; }
}
表单实际上将作为“Course”模型提交,而不是“CourseTemplates”模型,“Course”模型比“CourseTemplates”模型有更多的数据字段——例如:
public DateTime StartDate { get; set; }
public bool Approved { get; set; }
etc. . .
就用户体验而言,我想到的是管理员会事先检查并添加许多可能的课程选项,这只是为了简化大多数教师的申请流程(因此他们不必为他们申请教的每一堂课),但我希望老师能够在提交课程以供管理员审查之前编辑任何信息。
有小费吗?