Cakephp updating related data

Cakephp updating related data

Because it's the primary key, it's displayed, but it can't be changed. Updates the retrieved Instructor entity with values from the model binder. Therefore you need to exclude the Courses property from model binding.

The relationship between the Course and Instructor entities is many-to-many, which means you do not have direct access to the foreign key properties which are in the join table. Finally, the list is passed to the view in a ViewBag property. This doesn't require any change to the code that calls TryUpdateModel because you're using the whitelisting overload and Courses isn't in the include list. Replace the EditPost method with the following code, which calls a new method that updates the Courses navigation property of the Instructor entity.

Therefore you needUpdates the retrieved InstructorBecause it's the primary

This is the same as what you did in the HttpGet Edit method. Instead of using the model binder to update the Courses navigation property, you'll do that in the new UpdateInstructorCourses method. For each course, the code checks whether the course exists in the instructor's Courses navigation property.

It's setting up data for a drop-down list, but you what you need is a text box. None attribute means the user should be able enter the primary key value.

The approach taken here to edit instructor course data works well when there is a limited number of courses. The drop-down list sets the Course.

For most relationships, this can be done by updating either foreign key fields or navigation properties. In the last tutorial you'll add code that prevents that scenario from happening. Replace the HttpPost Edit method with the following code.

Since the view doesn't have a collection of Course entities, the model binder can't automatically update the Courses navigation property. In each column is a check box followed by a caption that consists of the course number and title. The changes are highlighted. You can make the application use web server resources more efficiently by implementing asynchronous code, and that's what you'll do in the next tutorial.