Show strict dropdowlist in my ASP.NET-MVC5 razor form
I need to show a strongly typed dropdowlist in my ASP.NET-MVC5 @ html.begin razor form. There are three model classes in this form. One is a course that is populated by the user and two more schools and campuses to be displayed in the dropdown. I created a ViewModel to combine all three and pass along with the school and campus the list of data in the object.
Now I need to show this razor shaped dropdown menu, how do I do it ???? I need to select the school and campus ID of the selected name from the dropdown.
controller
[HttpGet]
public ActionResult CreateStudentCourse()
{
var _studentCourseModel = new StudentCourse_ViewModel
{
_schoolList = _studentProfileServices.GetAllSchools(),
_CampusList = _studentProfileServices.GetAllCampus()
};
return PartialView("CreateStudentCourse_Partial", _studentCourseModel);
}
ViewModel
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; }
}
School model
[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; }
}
Campus model
[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 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 Town ")]
public string Town { get; set; }
[MaxLength(150)]
[Display(Name = "PostCode")]
[Required(ErrorMessage = "Require Campus PostCode")]
public string PostCode { get; set; }
//public virtual Course Course { get; set; }
}
Partial view of a razor
@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???????
+3
source to share
2 answers
I found an easier solution, post the ie class model to my user course to populate and create a selectBist viewBag object and use as a razor ..
controller
[HttpGet]
public ActionResult CreateStudentCourse()
{
ViewBag.Schools = new SelectList(_studentProfileServices.GetAllSchools(), "SchoolID", "Title");
ViewBag.Campus = new SelectList(_studentProfileServices.GetAllCampus(), "CampusID", "Site");
return PartialView("CreateStudentCourse_Partial");
}
My kind
<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>
0
source to share