0

I'm new to MVC 3 and Entity Framework so I'd like to know what is the best approach.

Basically, I have 3 entities: Course, Module and Chapter. Each is a parent of the next with a one to many relationship (A Course has many Modules and a Module has many Chapters). I have a column SortOrder for Modules and Chapters to have them ordered sequentially.

My idea was is to use partial views for the child entities when updating the parent.

I have 3 views in mind:

  1. Create/Update Course: all basic details for a course
  2. Course Modules (basically a different view for Update Course) which has an option to add multiple partial views, each creating a Module
  3. Course Timeline (still a different view for update course) which lists all Modules (on separate divs) and has the option to add multiple partial views, each creating a Chapter

Does my plan sound right and plausible? I plan to use hidden fields to store IDs. I also want the saves to occur asynchronously.

Any piece of advise or information would be highly appreciated. Thanks!

AnimaSola
  • 7,146
  • 14
  • 43
  • 62
  • 2
    Your question is a bit vague and unspecific. It sounds all reasonable and is certainly doable. I suggest that you just start the project and ask more specific questions when you get stuck with design or implementation. – Slauma Mar 07 '13 at 21:27
  • The only advice I would add is that if you plan to do things asynchronously, you should make sure you are familiar with JQuery – StanK Mar 08 '13 at 04:25

1 Answers1

1

I think this is what your after but not sure. For handling persistence of child/grandchild entities, you can do this in several ways. You can either perform crud operations on each entity separately. So that will involve for example saving the modules by themselves with a reference to the course, probably courseId.

Or you can look at saving just the aggregate root, which in this case looks like its your Course entity. This will involve Loading the course, populating the modules on the course, and for each module populate the chapters. Then when you `db.Courses.Add(newCourse); db.SaveChanges(); all the entities will be persisted. You have to make sure your foreign key and model references are setup correctly.

For example, to save child entities:

public ActionResult DoSomething(int courseId, Module newModule)
{
    var course = someService.LoadCourse(courseId);
    course.Modules.Add(newModule);

    using (var db = new MyDbContext())
    {
        db.Courses.Add(course);
        db.SaveChanges();
    }

    return RedirectToAction("Success");
}

Or you can save individually:

public ActionResult DoSomething(Module newModule)
{
    using (var db = new MyDbContext())
    {
        //You will need to make sure newModule.CourseId is set correctly
        db.Modules.Add(newModule);
        db.SaveChanges();
    }

    return RedirectToAction("Success");
}

Depending on your views, you will be able to judge which way is best to go. Regarding asynchronous saving, you will be able to call these endpoints with jquery posting the models as json. On a side note, one thing to look at would be to create a custom Anti Forgery Token validator for json requests, example.

Community
  • 1
  • 1
gdp
  • 8,032
  • 10
  • 42
  • 63