我需要在ASP.NET-MVC5 razor @html.begin表单中显示强类型的dropdowlist。这种形式涉及三种模型类。一个是用户填写的课程以及需要在下拉列表中显示的另外两个学校和校园。我创建了ViewModel来组合所有这三个并传递对象中的学校和校园数据列表。
现在我需要以剃刀形式显示这个下拉列表,我是怎么做到的?我需要从下拉列表中选择学校的ID和所选标题的校园。
[HttpGet]
public ActionResult CreateStudentCourse()
{
var _studentCourseModel = new StudentCourse_ViewModel
{
_schoolList = _studentProfileServices.GetAllSchools(),
_CampusList = _studentProfileServices.GetAllCampus()
};
return PartialView("CreateStudentCourse_Partial", _studentCourseModel);
}
public class StudentCourse_ViewModel
{
public StudentCourse_ViewModel() { }
[Key]
[Display(Name = "Course ID")]
public int CourseID { get; set; }
[Display(Name = "Student ID")]
[Required(ErrorMessage = "Require Your Student ID")]
public int StudentID { get; set; }
[Display(Name = "School ID")]
[Required(ErrorMessage = "Require Your School Title")]
public int SchoolID { get; set; }
[Display(Name = "Campus ID")]
[Required(ErrorMessage = "Require Your Campus Title")]
public int CampusID { get; set; }
[Display(Name = "Course Title")]
[MaxLength(150)]
[Required(ErrorMessage = "Require Your Course Title")]
public string CourseTitle { get; set; }
[Display(Name = "Mode Of Study")]
[Required(ErrorMessage = "Require Mode Of Study")]
[MaxLength(50)]
public string ModeOfStudy { get; set; }
[Display(Name = "Study Level")]
[MaxLength(50)]
[Required(ErrorMessage = "Require Your Study Level")]
public string StudyLevel { get; set; }
[Display(Name = "Date Of Course Start")]
[Required(ErrorMessage = "Require Date of Your Start")]
public System.DateTime DateOfCourseStart { get; set; }
[Display(Name = "Year Of Study")]
[MaxLength(10)]
[Required(ErrorMessage = "Require Your Year Of Study")]
public string YearOfStudy { get; set; }
// following are from another class model
public List<School> _schoolList { get; set; }
public List<Campus> _CampusList { get; set; }
}
[Table("School")]
public class School
{
public School() { }
[Key]
[Display(Name = "School ID")]
public int SchoolID { get; set; }
[MaxLength(150)]
[Display(Name = "School Title")]
[Required(ErrorMessage = "Require School Title")]
public string Title { get; set; }
// public Course Course { get; set; }
}
[Table("Campus")]
public class Campus
{
public Campus() { }
[Key]
[Display(Name = "Campus ID")]
public int CampusID { get; set; }
[MaxLength(150)]
[Display(Name = "Site")]
[Required(ErrorMessage = "Require Site Title")]
public string Site { get; set; }
[MaxLength(150)]
[Display(Name = "Region")]
[Required(ErrorMessage = "Require Campus's Region")]
public string Region { get; set; }
[MaxLength(250)]
[Display(Name = "Address")]
[Required(ErrorMessage = "Require Campus' Address ")]
public string Address { get; set; }
[MaxLength(250)]
[Display(Name = "Town")]
[Required(ErrorMessage = "Require Campus's Town ")]
public string Town { get; set; }
[MaxLength(150)]
[Display(Name = "PostCode")]
[Required(ErrorMessage = "Require Campus's PostCode")]
public string PostCode { get; set; }
//public virtual Course Course { get; set; }
}
@model App.DAL.Model.StudentCourse_ViewModel
@using (Html.BeginForm("CreateStudentCourse", "StudentProfile", FormMethod.Post, new { id = "CreateStudentCourseForm" }))
{ ............my code here ...
I need drop down for _schoolList and _CampusList???????
答案 0 :(得分:1)
学校:
@Html.DropDownListFor(m => m.SchoolID, new SelectList(_schoolList, "SchoolID ", "Title", Model.SchoolID))
校园:
@Html.DropDownListFor(m => m.CampusID, new SelectList(_CampusList, "CampusID ", "Site", Model.CampusID))
答案 1 :(得分:0)
我找到了更简单的解决方案,发送类的模型,即在我的案例中为用户填写它并创建viewBag selectList对象并在剃刀视图中使用..
[HttpGet]
public ActionResult CreateStudentCourse()
{
ViewBag.Schools = new SelectList(_studentProfileServices.GetAllSchools(), "SchoolID", "Title");
ViewBag.Campus = new SelectList(_studentProfileServices.GetAllCampus(), "CampusID", "Site");
return PartialView("CreateStudentCourse_Partial");
}
<div class="form-group">
@Html.Label("Your School", htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownList("Schools", null, "Select School", new { id ="schoolList", @class = "form-control" })
</div>
</div>