With previous ASP.NET MVC preview releases you had to
manually add views through the Project->Add New Item dialog in VS, and
creating and wiring up everything required several manual steps (making sure
the directory/file structure is right, going into the code-behind file to
specify the strongly typed ViewData model type, etc).
Today's beta makes the steps much easier. You can now
just move your source editor cursor to be within a Controller action method in
the source editor, and then right-click and select a new "Add View"
context menu item (alternatively you can type the Ctrl-M Ctrl-V keyboard
shortcut to invoke this without having to take your hands off the keyboard):
This will bring up a new "Add View"
dialog that allows you to specify the name of the view you want to create, its
master page, and optionally its strongly typed ViewData "Model" type:
Visual Studio will automatically pre-populate
the view name based on the action method your cursor is within (you can then
override this if you want). For example, if our cursor had been within an
"Edit" action method when we selected "add view" it would
have pre-populated the view name textbox with "Edit" instead of
The strongly typed ViewData "model"
for a view can be selected from an editable ComboBox that lists all classes in
(or referenced) from the MVC project:
You can either select a type from the list, or
manually type one in the ComboBox. You can also optionally pick an
initial type from the list and then tweak it. For example, we could
select the "Product" class from the list and then use the ComboBox
editing support to wrap it as an IEnumerable<Product> - meaning a
sequence of products:
When we click the "Add" button,
Visual Studio will automatically create the appropriate view directory
structure, and add a strongly typed view with the right name and base class to
our project. For example, if I followed the steps above it would create a
new \Views\Products directory for me (since my controller class name is
"ProductsController") and add the strongly-typed "Browse.aspx"
view to it (which derives from ViewPage<IEnumerable<Product>> -
since that was the model type we indicated in the dialog above):
The newly created view will automatically be
opened for us in the IDE. We can then implement our view with full
intellisense (tip: make sure to do a build immediately after creating the view
to ensure that intellisense shows up for your strongly typed model):
And at runtime we will now have an SEO
optimized product browsing page built with ASP.NET MVC:
Note: The view file created by Add-View with this beta
release is empty. For the final release we are hoping to add some
"scaffolding" features to the Add-View dialog that will allow you to
optionally specify that you want to automatically create an HTML list/details
view or edit/insert form based on the strongly-typed model specified in the
add-view dialog (you can then start with this initial html view and tweak it
however you want). In the future we will also integrate ASP.NET Dynamic
Data with MVC to support even richer scaffolding options.