0

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>
}
AlexGH
  • 2,735
  • 5
  • 43
  • 76
  • 1
    You can use an editor template. Here is a post about a student-course checkbox usecase like yours http://stackoverflow.com/questions/38961222/how-to-know-the-selected-checkboxes-from-within-the-httppost-create-action-metho/38964032#38964032 – Shyju Aug 19 '16 at 13:10
  • Another example - [Pass List of Checkboxes into View and Pull out IENumerable](http://stackoverflow.com/questions/29542107/pass-list-of-checkboxes-into-view-and-pull-out-ienumerable/29554416#29554416) –  Aug 19 '16 at 23:26

1 Answers1

0

Thanks to those links I could did it, this is my final HTTPost Method:

 [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create( VMSalesman vmsalesman)
        {
            if (ModelState.IsValid)
            {
                List<Customer> list = new List<Customer>();
                for (int i = 0; i < vmsalesman.CustomerList.Count; i++)
                {
                    if (vmsalesman.CustomerList[i].IsSelected==true)
                    {
                        int n = vmsalesman.CustomerList[i].VMCustomerID;
                        list.Add(db.Customers.Where(c => c.CustomerID == n).First());
                    }   
                }
                Salesman salesman = new Salesman() {
                    Name = vmsalesman.Name,
                    LastName = vmsalesman.LastName,
                    Location = vmsalesman.Location,
                    CustomersList = list
                };
                db.Salesmen.Add(salesman);
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            return View(vmsalesman);
        }
AlexGH
  • 2,735
  • 5
  • 43
  • 76