Starting from:
$30

$24

Lab #5 Solution

Introduction
Problem
For this lab you will be moving the existing lab to the web as an ASP.NET MVC application. Only the UI will be changed in this lab. All the existing code should continue to work unchanged.

Solution
You will be removing the existing Winforms project and adding a new ASP.NET MVC project. No other changes to the existing code should be needed.

Note: All screenshots are for demonstration purposes. You can make adjustments to meet your needs.

Setting Up the Solution
As with previous labs, copy the entire solution (folder and all) into a new folder. This will allow you to reuse all your existing code but still keep it separate from previous lab assignments.

Remove the Winforms UI project from the solution and folder. It is no longer needed.

Creating the Web Project
Add a new ASP.Net Web Application project to the solution called MovieLib.Web.





In the wizard select the MVC template, verify the Authentication option is No Authentication and do not generate unit tests.




Set the project as the startup project.
Add references to the core project assemblies.

MovieLib
MovieLib.Data.Sql
Remove references to the following assemblies.

Microsoft.AI.*
Microsoft.ApplicationInsights
System.Web.ApplicationServices
System.Web.DynamicData
System.Web.Entity
System.Web.Extensions
System.Web.Services
System.Web.WebPages.Deployment
Delete the following items from the project.

App_Data folder
ApplicationInsights.config
Open the web.config file.

Copy the connectionStrings section from the existing Winforms project
Remove the ApplicationInsightsWebTracking module from the following sections

System.web\httpModules
System.webServer\modules
Build and run the project to ensure it loads.
Cleaning Up Home
Edit the Index view for Home.

Replace the contents of the DIV element with the class jumbotron.

<h2Welcome to MovieLib Web</h2
Replace the DIV element with the class row.

<divYour name</div
<divITSE 1430</div
Edit the _Layout view under Views\Shared.

Change the title from My ASP.NET Application to MovieLib in the header.
Remove the About and Contact links in the navbar.
Change the copyright notice in the footer to contain your name.
Change the link with text Application Name to MovieLib.
Edit the HomeController.

Remove the actions for everything but Index.
Defining the Models
To isolate the UI from the business layer you need to add a model for each type you want to expose. In the web project do the following.

Add a new class called MovieViewModel to the Models folder.
Add properties for each of the properties already defined on the Movie business object.
Implement IValidableObject
Implement the IValidatableObject interface. Instead of putting the logic in the Validate method, use attributes so the client can enforce the rules. For the Length property use a custom error

To simplify the controller logic, create conversion methods to convert between Movie and MovieViewModel types. Use extension methods.

Showing the Movies
Implement the Action
Add a new, empty MVC controller class called MovieController to manage the movies. Rename the Index action to List. Implement the action.

Query the movies from the SQL database (consider using a helper method to get the database).
For each movie, convert to a view model.
Sort the movies by title.
Pass the view models to the view.
Implement the View
Right click inside the action to add the corresponding view.
The view should be a List template using MovieViewModel as the model to render the list.
Change the Create New to Add and the action to Add.
Remove the Details link.
Change the view header and title to Movies.
Expose on Home Page
Add a link to the layout page in the navbar to access the new view. Call the link Movies.





Adding Movies
Implement the GET Action
Add a new Add action to the Movie controller.
Create an empty view model.
Pass the view model to the view.
Implement the POST Action
Add a new Add action to the Movie controller that accepts a MovieViewModel.
Mark the action as a POST action.
Verify the model is valid using ModelState.
If valid then add the movie to the database.
Catch any exceptions. If any exception occurs add it to ModelState. Reminder that errors will not be shown if you use the overload accepting an Exception object.
If validation or any errors occur then return the user to the view with the existing model.
If the operation is successful redirect the user back to the list view.
Implement the View
Right click inside the action to add the corresponding view.
The view should be an Create template using MovieViewModel as the model.
Change the view header and title to Add Movie.
Remove the MovieViewModel header element.
The description can be long so replace the EditorFor method with TextAreaFor.
Fix the back link to use the correct action name.
Verify the behavior
Can navigate to the view from the list view.
Entering an empty title or invalid length causes an error to appear.
Any server-side errors (i.e. adding a duplicate movie) returns the user to the view with the existing data intact and the error displayed.
Successfully adding a movie returns to the list view and the movie appears.
Editing Movies
Implement the GET Action
Add a new Edit action to the Movie controller. The action should accept an id parameter representing the movie ID.
Query the movie from the database.
If the movie does not exist return a 404.
If the movie does exist then pass the view model to the view.
Implement the POST Action
Add a new Edit action to the Movie controller that accepts a MovieViewModel.
Mark the action as a POST action.
Verify the model is valid using ModelState.
If valid then update the movie in the database.
Catch any exceptions. If any exception occurs add it to ModelState.
If validation or any errors occur then return the user to the view with the existing model.
If the operation is successful redirect the user back to the list view.
Implement the View
Right click inside the action to add the corresponding view.
The view should be an Edit template using MovieViewModel as the model.
Change the view header and title to the name of the movie being edited.
Remove the MovieViewModel header element.
The description can be long so replace the EditorFor method with TextAreaFor.
Fix the back link to use the correct action name.
Verify the behavior
Can navigate to the view from the list view.
The selected movie populates the controls.
Entering an empty title or invalid length causes an error to appear.
Any server-side errors (i.e. adding a duplicate movie) returns the user to the view with the existing data intact and the error displayed.
Successfully updating a movie returns to the list view and the movie appears.
Deleting Movies
Implement the GET Action
Add a new Delete action to the Movie controller. The action should accept an id parameter representing the movie ID.
Query the movie from the database.
If the movie does not exist return a 404.
If the movie does exist then pass the view model to the view.
Implement the POST Action
Add a new Delete action to the Movie controller that accepts a MovieViewModel.
Mark the action as a POST action.
Delete the movie from the database.
Redirect back to the list view.
Implement the View
Right click inside the action to add the corresponding view.
The view should be an Delete template using MovieViewModel as the model.
Change the view header and title to the name of the movie being edited.
Remove the MovieViewModel header element.
Fix the back link to use the correct action name.
Verify the behavior
Can navigate to the view from the list view.
The selected movie populates the controls.
The movie is deleted from the list.
Requirements
Your program must meet all the following requirements.

Compile cleanly with no warnings or errors.
Meet all the points mentioned in the solution.
Ensure each file has a file header indicating the course, your name and date.
Solution, project and code should be uploaded to Github
Submit lab in MyTCC by providing link to Github URL

More products