You can now type Ctrl-M, Ctrl-V within a
Controller action method, or right-click within an action method and choose the
“Add View” context menu item to create new view templates:
Figure 4
-
This will cause an “Add View” dialog to appear
that allows you to name and create a new view (it is pre-populated with
convention-based options). It allows you to create “empty” view
templates, or automatically generate/scaffold view templates that are based on
the type of object passed to the view by the Controller action method.
The scaffolding infrastructure uses reflection when creating view templates –
so it can scaffold new templates based on any POCO (plain old CLR object)
passed to it. It does not have a dependency on any particular ORM or data
implementation.
For example, below we are indicating that we
want to scaffold a “List” view template based on the sequence of Product
objects we are passing from our action method above:
Figure 5
Clicking the “Add” button will cause a view template to be
created for us within the \Views\Products\ directory with a default “scaffold”
implementation:
Figure 6
We can then run our application and request
the /products URL within our browser to see a listing of our retrieved
products:
Figure 7
The RC ships with a number of built-in
scaffold templates: “Empty”, “List”, “Details”, “Edit” and “Create” (you can
also add your own scaffold templates – more details on this in a moment).
For example, to enable product editing support
we can implement the HTTP-GET version of our “Edit” action method on our
Products controller like below and then invoke the “Add View”
Figure 8
Within the “Add View” dialog we can indicate we are passing
a “Product” object to our view and choose the “Edit” template option to
scaffold it:
Figure 9
Clicking the “Add” button will cause an edit view template
to be created with a default scaffold implementation within the
\Views\Products\ directory
Figure 10
We can then run our application and request
the /products/edit/1 URL within our browser to edit the Product details:
Figure 11
To save edit changes we can implement the HTTP-POST version
of our “Edit” action method on our Products controller:
Figure 12
Notice in the code above how in the case of an
error (for example: someone enters a bogus string for a number value) we
redisplay the view. The “edit” and “create” scaffold templates contain
the HTML validation helper methods necessary to preserve user input and flag
invalid input elements in red when this happens:
Figure 13
You’ll rarely end up using a scaffold-created template
exactly as-is, and often will end up completely replacing it. But being
able to get an initial implementation up and running quickly, and having an
initial view template for your scenario that you can then easily tweak is
really useful.
Because the scaffold infrastructure supports scaffolding
views against any plain-old CLR object, you can use it with both domain model
objects (including those mapped with LINQ to SQL, LINQ to Entities, nHibernate,
LLBLGen Pro, SubSonic, and other popular ORM implementations) as well as to
create scaffolds with custom Presentation Model/ViewModel classes.