Re image store in database in binary format using mvc 4 and entity framework


The HttpPost methods for both Create and Edit also include code that sets the selected item when they redisplay the page after an error:. This code ensures that when the page is redisplayed to show the error message, whatever department was selected stays selected.

Normally the scaffolder doesn't scaffold a primary key because the key value is generated by the database and can't be changed and isn't a meaningful value to be displayed to users. For Course entities the scaffolder does include an text box for the CourseID field because it understands that the DatabaseGeneratedOption.

None attribute means the user should be able enter the primary key value. But it doesn't understand that because the number is meaningful you want to see it in the other views, so you need to add it manually.

Because it's the primary key, it's displayed, but it can't be changed. There's already a hidden field Html. HiddenFor helper for the course number in the Edit view. LabelFor helper doesn't eliminate the need for the hidden field because it doesn't cause the course number to be included in the posted data when the user clicks Save on the Edit page. Run the Create page display the Course Index page and click Create New and enter data for a new course:. The Course Index page is displayed with the new course added to the list.

The department name in the Index page list comes from the navigation property, showing that the relationship was established correctly. Run the Edit page display the Course Index page and click Edit on a course. Change data on the page and click Save. The Course Index page is displayed with the updated course data.

When you edit an instructor record, you want to be able to update the instructor's office assignment. The Instructor entity has a one-to-zero-or-one relationship with the OfficeAssignment entity, which means you must handle the following situations:.

The scaffolded code here isn't what you want. It's setting up data for a drop-down list, but you what you need is a text box. Replace this method with the following code:. This code drops the ViewBag statement and adds eager loading for the associated OfficeAssignment entity. You can't perform eager loading with the Find method, so the Where and Single methods are used instead to select the instructor.

Replace the HttpPost Edit method with the following code. Gets the current Instructor entity from the database using eager loading for the OfficeAssignment navigation property. This is the same as what you did in the HttpGet Edit method. Updates the retrieved Instructor entity with values from the model binder. The TryUpdateModel overload used enables you to whitelist the properties you want to include. This prevents over-posting, as explained in the second tutorial.

If the office location is blank, sets the Instructor. OfficeAssignment property to null so that the related row in the OfficeAssignment table will be deleted. Run the page select the Instructors tab and then click Edit on an instructor. Change the Office Location and click Save. Instructors may teach any number of courses. Now you'll enhance the Instructor Edit page by adding the ability to change course assignments using a group of check boxes, as shown in the following screen shot:.

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. Instead, you add and remove entities to and from the Instructor.

The UI that enables you to change which courses an instructor is assigned to is a group of check boxes. A check box for every course in the database is displayed, and the ones that the instructor is currently assigned to are selected. The user can select or clear check boxes to change course assignments. If the number of courses were much greater, you would probably want to use a different method of presenting the data in the view, but you'd use the same method of manipulating navigation properties in order to create or delete relationships.

To provide data to the view for the list of check boxes, you'll use a view model class. The changes are highlighted. The code adds eager loading for the Courses navigation property and calls the new PopulateAssignedCourseData method to provide information for the check box array using the AssignedCourseData view model class.

The code in the PopulateAssignedCourseData method reads through all Course entities in order to load a list of courses using the view model class. For each course, the code checks whether the course exists in the instructor's Courses navigation property. To create efficient lookup when checking whether a course is assigned to the instructor, the courses assigned to the instructor are put into a HashSet collection.

The Assigned property is set to true for courses the instructor is assigned. The view will use this property to determine which check boxes must be displayed as selected. Finally, the list is passed to the view in a ViewBag property. Next, add the code that's executed when the user clicks Save.

Replace the EditPost method with the following code, which calls a new method that updates the Courses navigation property of the Instructor entity. The method signature is now different from the HttpGet Edit method, so the method name changes from EditPost back to Edit. Since the view doesn't have a collection of Course entities, the model binder can't automatically update the Courses navigation property.

Instead of using the model binder to update the Courses navigation property, you'll do that in the new UpdateInstructorCourses method. Therefore you need to exclude the Courses property from model binding. 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. If no check boxes were selected, the code in UpdateInstructorCourses initializes the Courses navigation property with an empty collection:.

The code then loops through all courses in the database and checks each course against the ones currently assigned to the instructor versus the ones that were selected in the view. To facilitate efficient lookups, the latter two collections are stored in HashSet objects.

If the check box for a course was selected but the course isn't in the Instructor. Courses navigation property, the course is added to the collection in the navigation property.

If the check box for a course wasn't selected, but the course is in the Instructor. Courses navigation property, the course is removed from the navigation property. After you paste the code, if line breaks and indentation don't look like they do here, manually fix everything so that it looks like what you see here. This code creates an HTML table that has three columns. In each column is a check box followed by a caption that consists of the course number and title.

The check boxes all have the same name "selectedCourses" , which informs the model binder that they are to be treated as a group. The value attribute of each check box is set to the value of CourseID. But it doesn't understand that because the number is meaningful you want to see it in the other views, so you need to add it manually. Because it's the primary key, it's displayed, but it can't be changed. There's already a hidden field Html. HiddenFor helper for the course number in the Edit view.

LabelFor helper doesn't eliminate the need for the hidden field because it doesn't cause the course number to be included in the posted data when the user clicks Save on the Edit page. Run the Create page display the Course Index page and click Create New and enter data for a new course:. The Course Index page is displayed with the new course added to the list. The department name in the Index page list comes from the navigation property, showing that the relationship was established correctly.

Run the Edit page display the Course Index page and click Edit on a course. Change data on the page and click Save. The Course Index page is displayed with the updated course data.

When you edit an instructor record, you want to be able to update the instructor's office assignment. The Instructor entity has a one-to-zero-or-one relationship with the OfficeAssignment entity, which means you must handle the following situations:. The scaffolded code here isn't what you want. It's setting up data for a drop-down list, but you what you need is a text box.

Replace this method with the following code:. This code drops the ViewBag statement and adds eager loading for the associated OfficeAssignment entity. You can't perform eager loading with the Find method, so the Where and Single methods are used instead to select the instructor.

Replace the HttpPost Edit method with the following code. Gets the current Instructor entity from the database using eager loading for the OfficeAssignment navigation property. This is the same as what you did in the HttpGet Edit method. Updates the retrieved Instructor entity with values from the model binder. The TryUpdateModel overload used enables you to whitelist the properties you want to include.

This prevents over-posting, as explained in the second tutorial. If the office location is blank, sets the Instructor. OfficeAssignment property to null so that the related row in the OfficeAssignment table will be deleted. Run the page select the Instructors tab and then click Edit on an instructor.

Change the Office Location and click Save. Instructors may teach any number of courses. Now you'll enhance the Instructor Edit page by adding the ability to change course assignments using a group of check boxes, as shown in the following screen shot:. 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.

Instead, you add and remove entities to and from the Instructor. The UI that enables you to change which courses an instructor is assigned to is a group of check boxes. A check box for every course in the database is displayed, and the ones that the instructor is currently assigned to are selected. The user can select or clear check boxes to change course assignments. If the number of courses were much greater, you would probably want to use a different method of presenting the data in the view, but you'd use the same method of manipulating navigation properties in order to create or delete relationships.

To provide data to the view for the list of check boxes, you'll use a view model class. The changes are highlighted. The code adds eager loading for the Courses navigation property and calls the new PopulateAssignedCourseData method to provide information for the check box array using the AssignedCourseData view model class. The code in the PopulateAssignedCourseData method reads through all Course entities in order to load a list of courses using the view model class.

For each course, the code checks whether the course exists in the instructor's Courses navigation property. To create efficient lookup when checking whether a course is assigned to the instructor, the courses assigned to the instructor are put into a HashSet collection.

The Assigned property is set to true for courses the instructor is assigned. The view will use this property to determine which check boxes must be displayed as selected.

Finally, the list is passed to the view in a ViewBag property. Next, add the code that's executed when the user clicks Save.

Replace the EditPost method with the following code, which calls a new method that updates the Courses navigation property of the Instructor entity. The method signature is now different from the HttpGet Edit method, so the method name changes from EditPost back to Edit. Since the view doesn't have a collection of Course entities, the model binder can't automatically update the Courses navigation property.

Instead of using the model binder to update the Courses navigation property, you'll do that in the new UpdateInstructorCourses method. Therefore you need to exclude the Courses property from model binding. 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.

If no check boxes were selected, the code in UpdateInstructorCourses initializes the Courses navigation property with an empty collection:. The code then loops through all courses in the database and checks each course against the ones currently assigned to the instructor versus the ones that were selected in the view. To facilitate efficient lookups, the latter two collections are stored in HashSet objects. If the check box for a course was selected but the course isn't in the Instructor.

Courses navigation property, the course is added to the collection in the navigation property. If the check box for a course wasn't selected, but the course is in the Instructor.

Courses navigation property, the course is removed from the navigation property. After you paste the code, if line breaks and indentation don't look like they do here, manually fix everything so that it looks like what you see here.

This code creates an HTML table that has three columns. In each column is a check box followed by a caption that consists of the course number and title. The check boxes all have the same name "selectedCourses" , which informs the model binder that they are to be treated as a group. The value attribute of each check box is set to the value of CourseID. When the page is posted, the model binder passes an array to the controller that consists of the CourseID values for only the check boxes which are selected.

When the check boxes are initially rendered, those that are for courses assigned to the instructor have checked attributes, which selects them displays them checked. After changing course assignments, you'll want to be able to verify the changes when the site returns to the Index page.

Therefore, you need to add a column to the table in that page. In this case you don't need to use the ViewBag object, because the information you want to display is already in the Courses navigation property of the Instructor entity that you're passing to the page as the model.