I have two classes, Student
and Course
, I've made a one-to-many relationship between them using Entity Framework
.
When I'm going to create a new student I want to show a list of all the registered Courses
by name and then select from that list the corresponding courses to the Student
I'm creating, I want to display that using checkboxes.(I accept suggestions about do this in a different and more original way if you have)
Here are my classes:
public class Student: Person
{
public float Average { get; set; }
public virtual List<Course> Courses { get; set; }
}
public class Course
{
public int CourseID { get; set; }
[Required]
public string Name { get; set; }
public int Years { get; set; }
public float Difficulty { get; set; }
public int StudentID { get; set; }
public virtual Student Student { get; set; }
}
This is my get Method just to show the View:
public ActionResult Create()
{
ViewBag.ListCourses = db.Courses.ToList();
return View();
}
And the View:
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
@foreach (var item in ViewBag.ListCourses)
{
//Checkbox or CheckboxFor???? What goes here???...
Html.CheckBox();
}
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Create" class="btn btn-default" />
</div>
</div>
}