ASP.NET MVC 3 Helpers, Forms and Validation

Version: 1.1.0

Description

In ASP.NET MVC Models and Data Access Hand-on Lab, you have been loading data from the database and displaying it. In this Hands-on Lab, you will add to the Music Store application, the ability to edit that data.

Overview

Note:
This Hands-on Lab assumes you have basic knowledge of ASP.NET MVC. If you have not used ASP.NET MVC before, we recommend you to go over ASP.NET MVC Fundamentals Hand-on Lab.

In ASP.NET MVC Models and Data Access Hand-on Lab, you have been loading and displaying data from the database. In this Hands-on Lab, you will add to the Music Store application the ability to edit that data.

With that goal in mind, you will first create the controller that will support the Create, Read, Update and Delete (CRUD) actions of albums. You will generate an Index View template taking advantage of ASP.NET MVC’s scaffolding feature to display the albums’ properties in an HTML table. To enhance that view, you will add a custom HTML helper that will truncate long descriptions.

Afterwards, you will add the Edit and Create Views that will let you alter the albums in the database, with the help of form elements like dropdowns.

Lastly, you will let users delete an album and also you will prevent them from entering wrong data by validating their input.

Objectives

In this Hands-On Lab, you will learn how to:

  • Create a controller to support CRUD operations
  • Generate an Index View to display entity properties in an HTML table
  • Add a custom HTML helper
  • Create and customize an Edit View
  • Differentiate between action methods that react to either HTTP-GET or HTTP-POST calls
  • Add and customize a Create View
  • Handle the deletion of an entity
  • Validate user input

System Requirements

You must have the following items to complete this lab:

  • ASP.NET and ASP.NET MVC 3
  • Visual Studio 2010 Express
  • SQL Server Database (Express edition or above)
    Note:
    You can install the previous system requirements by using the Web Platform Installer 3.0: https://go.microsoft.com/fwlink/?LinkID=194638.

Setup

Installing Code Snippets

For convenience, much of the code you will be managing along this lab is available as Visual Studio code snippets. To install the code snippets run .\Source\Assets\CodeSnippets.vsi file.

Exercises

This Hands-On Lab is comprised by the following exercises:

  1. Exercise 1: Creating the Store Manager controller and its Index view
  2. Exercise 2: Adding an HTML Helper
  3. Exercise 3: Creating the Edit View
  4. Exercise 4: Adding a Create View
  5. Exercise 5: Handling Deletion
  6. Exercise 6: Adding Validation

Estimated time to complete this lab: 35 minutes.

Note:
Each exercise is accompanied by an End folder containing the resulting solution you should obtain after completing the exercises. You can use this solution as a guide if you need additional help working through the exercises.

Next Step

Exercise 1: Creating the Store Manager controller and its Index view