My task is to create a .NET MVC Form connected to local MS SQL server. The form should care for registering data for Students and Courses. In the DB there is simple tables Students, StudentsCourses, Courses, and Teachers. Since I have to gain information for more than one Model, I'd created a ViewModel, Here it is :
public class ViewModelVM
{
public Teacher Teacher { get; set; }
public Student Student { get; set; }
public Cours Cours { get; set; }
}
In the database, I've already have some Courses, and my goal is to create DropDownList, so the Students must choose among them.
@using (Html.BeginForm())
{
@Html.ValidationSummary()
<div class="form-group">
<label>Student Firstname:</label>
@Html.TextBoxFor(x => x.Student.FirstName, new { @class = "formcontrol" })
</div>
<div class="form-group">
<label>Student Birthdate:</label>
@Html.TextBoxFor(x => x.Student.BirthDate, new { @class = "formcontrol" })
</div>
<div class="form-group">
<label>Student FNum:</label>
@Html.TextBoxFor(x => x.Student.Fnum, new { @class = "formcontrol" })
</div>
**<div class="form-group">
<label>Studet's Courses</label>
// I want to make something like this
// @Html.DropDownListFor(x => x.Student.Courses, new[] {
new SelectListItem() {Text = "C++", Value = bool.TrueString},
new SelectListItem() {Text = "c#", Value = bool.FalseString}},
"Choose course", new { @class = "formcontrol" })
</div>**
<div class="form-group">
<label>Teacher Fnum:</label>
@Html.TextBoxFor(x => x.Teacher.Fnum, new { @class = "formcontrol" })
</div>
<div class="form-group">
<label>Course Signature:</label>
@Html.TextBoxFor(x => x.Cours.Signature, new { @class = "formcontrol" })
</div>
<div class="form-group">
<label>Course.Teacher Fnum data:</label>
@Html.TextBoxFor(x => x.Cours.Teacher.Fnum, new { @class = "formcontrol" })
</div>
<div class="text-center">
<input class="btn btn-success" type="submit"
value="Submit Info" />
</div>
}
Important! : Items in the DropDownList wont be new, they will exist in the Database already.
My question is how exactly I can write this DropDown, because it is not correct now. Value = bool.TrueString
, is just example text, i would like to have something like : Model.Student.Courses.Where(CourseName == 'C++');