1

I have a situation where I have a many to many relationship in my model between Student and Lesson. In most cases Lessons are 1 on 1 where a single student attends the lesson but there are situations where a lesson is shared by more than one student (hence the many to many).

So my ViewModel looks a little like this:

public class ScheduleViewModel
{
    public Lesson Lesson { get; set; }
    public List<StudentViewModel> Students { get; set; }
    public List<StudentViewModel> AllStudents { get; set; }
}

In the View I would bind the Lesson properties using DisplayFor to show the details of the Lesson. Then I would have a DropDownListFor which uses AllStudents as its source. When a user selects a Student from the list a second DropDown would be generated (etc) allowing further Students to be added. These selections will be added to the Students property of my ViewModel.

In the past I would have handled this is javascript and managed the updating via JSON and AJAX. My instinct is that I should be using MVC Templates for this but in my research I've not found an example that has this exact scenario (or of course I may be barking up the wrong tree).

I have no code for my View at the moment as I'm really stuck on the right strategy to use in this case.

I hope that makes sense and any help on this greatly appreciated.

Many Thanks Simon.

1
  • What more than Student class is there inside of StudentViewModel ?
    – Lars
    Feb 15, 2014 at 16:26

1 Answer 1

1

Your Lesson class should have a binding for Students, something like this:

Lesson.cs

public class Lesson
{
    public int Id { get; set; }

    public string Name { get; set; }

    public virtual List<Student> Students { get; set; }
}

Then in your Student class you would bind a relation to lessons:

public class Student
{
    public int Id { get; set; }

    public string Name { get; set; }

    public virtual List<Lesson> Lessons { get; set; }
}

Now you have a many-to-many relationship between Lesson and Student.

Your viewmodel would then look more like this:

public class ScheduleViewModel
{
    public Lesson Lesson { get; set; }

    // All students property
    public List<Student> Students { get; set; }
}

Your Lesson property now contains all the information about your lesson, including which Students are connected to it, because it has a List of Students. You want to add the selected students from your view to this list.

The Students property is used to display all students in the database.

1
  • Hi Lars .. of course you are right. As my model is EF generated from the DB Student does contain the relation to lessons, but my StudentViewModel only contains data for the Student entity (not the related Lessons). Of course the ViewModel does from my original question although indirectly. Maybe I didn't make it clear in question but the main issue I have is adding those Student dropdown to the UI. I would have done this via jquery and AJAX in the past but I was wondering if there was an MVC way of doing this in the view; client side adding objects to a ViewModel. Feb 15, 2014 at 21:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.