Thư viện tri thức trực tuyến
Kho tài liệu với 50,000+ tài liệu học thuật
© 2023 Siêu thị PDF - Kho tài liệu học thuật hàng đầu Việt Nam

Tài liệu Intro to ASP.net MVC 4 With Visual Studio doc
Nội dung xem thử
Mô tả chi tiết
Intr
Visu
Rick A
Summa
applicat
version o
Categor
Applies
Source:
E-book
115 pag
ro to
ual S
Anderso
ry: This tuto
ion using M
of Microsof
ry: Step-By
to: ASP.NE
ASP.NET si
publicatio
es
o ASP
Studio
on and S
orial will te
Microsoft Vi
ft Visual Stu
-Step
ET MVC 4 B
te (link to s
on date: Ma
P.NET
o (Be
Scott Ha
ach you the
isual Studio
udio.
eta, Visual
source cont
ay 2012
T MV
eta)
anselma
e basics of
o 11 Express
Studio 11 B
tent)
VC 4 w
an
building an
s Beta for W
Beta
with
n ASP.NET M
Web, which
VC Web
is a free
Copyright © 2012 by Microsoft Corporation
All rights reserved. No part of the contents of this book may be reproduced or transmitted in any form or by any means
without the written permission of the publisher.
Microsoft and the trademarks listed at
http://www.microsoft.com/about/legal/en/us/IntellectualProperty/Trademarks/EN-US.aspx are trademarks of the
Microsoft group of companies. All other marks are property of their respective owners.
The example companies, organizations, products, domain names, email addresses, logos, people, places, and events
depicted herein are fictitious. No association with any real company, organization, product, domain name, email address,
logo, person, place, or event is intended or should be inferred.
This book expresses the author’s views and opinions. The information contained in this book is provided without any
express, statutory, or implied warranties. Neither the authors, Microsoft Corporation, nor its resellers, or distributors will
be held liable for any damages caused or alleged to be caused either directly or indirectly by this book.
Contents
Getting Started........................................................................................................................................3
What You'll Build...........................................................................................................................3
Skills You'll Learn...........................................................................................................................5
Getting Started..............................................................................................................................6
Creating Your First Application ....................................................................................................7
Adding a Controller ............................................................................................................................ 13
Adding a View ..................................................................................................................................... 20
Changing Views and Layout Pages ............................................................................................ 25
Passing Data from the Controller to the View........................................................................... 31
Adding a Model .................................................................................................................................. 37
Adding Model Classes................................................................................................................. 37
Creating a Connection String and Working with SQL Server LocalDB .................................... 41
Accessing Your Model's Data from a Controller............................................................................... 43
Creating a Movie ......................................................................................................................... 46
Examining the Generated Code.................................................................................................. 48
Strongly Typed Models and the @model Keyword.................................................................. 49
Working with SQL Server LocalDB ............................................................................................. 53
Examining the Edit Methods and Edit View ...................................................................................... 58
Processing the POST Request..................................................................................................... 65
Adding a Search Method and Search View............................................................................... 67
Displaying the SearchIndex Form............................................................................................... 67
Adding Search by Genre............................................................................................................. 77
Adding Markup to the SearchIndex View to Support Search by Genre .................................. 79
Adding a New Field to the Movie Model and Table......................................................................... 80
Adding a Rating Property to the Movie Model......................................................................... 80
Managing Model and Database Schema Differences............................................................... 82
Automatically Re-Creating the Database on Model Changes.................................................. 85
Adding Validation to the Model ........................................................................................................ 95
Keeping Things DRY.................................................................................................................... 95
Adding Validation Rules to the Movie Model ........................................................................... 95
Validation Error UI in ASP.NET MVC .......................................................................................... 97
How Validation Occurs in the Create View and Create Action Method ................................ 100
Adding Formatting to the Movie Model.................................................................................. 108
Examining the Details and Delete Methods.................................................................................... 111
Examining the Details and Delete Methods ............................................................................ 111
Wrapping Up ............................................................................................................................. 113
Getting Started
By Rick Anderson and Scott Hanselman
This tutorial will teach you the basics of building an ASP.NET MVC Web application using Microsoft Visual
Studio 11 Express Beta for Web, which is a free version of Microsoft Visual Studio. Before you start, make sure
you've installed the prerequisites listed below. You can install all of them by clicking the following link: Web
Platform Installer.
If you're using Visual Studio 11 Beta instead of Visual Studio 11 Express Beta for Web , install the prerequisites
by clicking the following link: Web Platform Installer
A Visual Web Developer project with C# source code is available to accompany this topic. Download the C#
version.
What You'll Build
You'll implement a simple movie-listing application that supports creating, editing, searching and listing movies
from a database. Below are two screenshots of the application you’ll build. It includes a page that displays a list
of movies from a database:
The application also lets you add, edit, and delete movies, as well as see details about individual ones. All dataentry scenarios include validation to ensure that the data stored in the database is correct.
Skills You'll Learn
Here's what you'll learn:
• How to create a new ASP.NET MVC project.
• How to create ASP.NET MVC controllers and views.
• How to create a new database using the Entity Framework Code First paradigm.
• How to retrieve and display data.
• How to edit data and enable data validation.
Getting Started
Start by running Visual Web Developer 11 Express Beta("Visual Web Developer" or VWD for short) and select
New Project from the Start page.
Visual Web Developer is an IDE, or integrated development environment. Just like you use Microsoft Word to
write documents, you'll use an IDE to create applications. In Visual Web Developer there's a toolbar along the
top showing various options available to you. There's also a menu that provides another way to perform tasks
in the IDE. (For example, instead of selecting New Project from the Start page, you can use the menu and
select File>New Project.)
Creating Your First Application
You can create applications using either Visual Basic or Visual C# as the programming language. Select Visual
C# on the left and then select ASP.NET MVC 4 Web Application. Name your project "MvcMovie" and then
click OK.
In the New ASP.NET MVC 4 Project dialog box, select Internet Application. LeaveRazor as the default view
engine.