HTML and CSS Reference
In-Depth Information
The
TextBoxFor()
helper function uses the metadata attributes to generate HTMl like this:
<input name="UserName" id="UserName" type="text"
data-val-required="The User name field is required."
data-val="true" value=""></input>
specifically, the
data-val
and
data-val-required
HTMl attributes are generated. The view also includes
these jQuery libraries:
<script src="~/Scripts/jquery.validate.min.js"></script>
<script src="~/Scripts/jquery.validate.unobtrusive.min.js"></script>
These Javascript libraries use the HTMl attributes such as data-val to perform client-side validation. For
more information, see the article at:
http://rachelappel.com/asp-net-mvc/how-data-annotations-for-asp-net-
Adding a Feedback Page
You will now create a feedback form and use this to demonstrate how to implement the new HTML5 capabilities.
You'll start by creating a model and then implement a strongly-typed view based on this model. You'll then add a
controller action as well as a link to the new page.
■
Adding a page to the web application usually involves adding a model, adding a view, and creating or
modifying a controller. The MVC pattern allows these to be developed separately and in a large project you will often
have different people responsible for the views and models. You may be able to use an existing model. However, in a
small project like this, where you are the sole developer, you will generally need to touch all three areas to add a page.
Tip
Creating the Feedback Model
A model defines the data elements that can be included on your page. By designing the model first, you can
simplify the view implementation.
In the Solution Explorer, right-click the Models folder and select the Add
➤
Class links and enter
FeedbackModel
for the class name. Click the OK button to create the class. For the class implementation, enter
the code shown in Listing 3-2.
■
The view files use the new Razor syntax and have the
.cshtml
(or .
vbhtml
) extension. However, the model
and controller files are standard C# (or Vb) classes.
Note
Listing 3-2.
The FeedbackModel class
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;