Introduction to MVC 4 05. A New Controller to Access the Model Data

advertisement
Introduction to MVC 4
05. A New Controller to Access the
Model Data
NTPCUG
Dr. Tom Perkins
In this Module
• Write a new Controller class
– Retrieves the Movie data
• CRUD (Create, Read, Update and Delete)
Views will be created automatically
• Make sure you have done a BUILD on the
application.
Build a new controller MoviesController
• Right-click the Controllers folder
• Create a new MoviesController controller
• Select the following options:
– Controller name: MoviesController. (This is the
default. )
– Template: MVC Controller with read/write actions and
views, using Entity Framework.
– Model class: Movie (MvcMovie.Models).
– Data context class: MovieDBContext
(MvcMovie.Models).
– Views: Razor (CSHTML). (The default.)
Click
Visual Studio creates:
• MoviesController.cs (in the Controllers folder)
• A Movies folder in the Views folder
• New CRUD files in the Views\Movies folder
–
–
–
–
–
Create.cshtml
Details.cshtml
Edit.cshtml
Delete.cshtml
Index.cshtml
• CRUD feature is created automatically
(scaffolding)
Run the Application
• Append /Movies to the URL in the address bar
• Control is routed to the default Index action
(method) in the Movies controller
• Same as http://localhost:xxxxx/Movies/Index
• Result: empty list of movies (no movies in the
database yet …)
Run the application
Add a Movie entity to the database
• Select Create
• Add some movie details
• Click Create
Click to add
Movie
Movie is displayed
• Make some
more entries!
IMDB Link
Amazon Movies
• Try Edit, Details,
and Delete
Let’s examine the generated code …
• Open Controllers\MoviesController.cs
• Examine the generated Index method:
public class MoviesController : Controller
{
private MovieDBContext db = new MovieDBContext();
//
// GET: /Movies/
Instantiates a movie
database context;
allows query, edit, and
delete on database
public ActionResult Index()
{
return View(db.Movies.ToList());
}
Passes list to
View
Returns a list
of all movies
Passing Data or Objects to a View
(2 ways)
• ViewBag object
– dynamic
– late-bound
• Pass strongly typed data or objects
– Provides compile-time checking
– Richer Intellisense
– Used by scaffolding mechanism (CRUD Views)
Passing Strongly Typed Objects
Movies controller Details method
public ActionResult Details(int id = 0)
{
Movie movie = db.Movies.Find(id);
if (movie == null)
{
return HttpNotFound();
}
return View(movie);
}
Instance of movie
model passed to
View
Views\Movies\Details.cshtml
(Partial )
@model MvcMovie.Models.Movie
Allows access of strongly typed Movie
Model
Used by DisplayNameFor and
DisplayFor HTML Helpers
Create and Edit methods and views also
pass a movie object
Passing Strongly Typed Objects
Index method in MoviesController.cs
public ActionResult Index()
{
return View(db.Movies.ToList());
}
Views\Movies\Index.cshtml
@model IEnumerable<MvcMovie.Models.Movie>
@foreach (var item in Model) {
<tr>
Display the returned List
<td>
@Html.DisplayFor(modelItem => item.Title)
as a table (grid) in the
</td>
Index View
<td>
@Html.DisplayFor(modelItem => item.ReleaseDate)
</td>
<td>
@Html.DisplayFor(modelItem => item.Genre)
</td>
<td>
@Html.DisplayFor(modelItem => item.Price)
</td>
<th>
@Html.DisplayFor(modelItem => item.Rating)
</th>
<td>
@Html.ActionLink("Edit", "Edit", new { id=item.ID }) |
@Html.ActionLink("Details", "Details", { id=item.ID }) |
@Html.ActionLink("Delete", "Delete", { id=item.ID })
</td>
</tr>
}
SQL SERVER LOCALDB
The Movies.mdf database
• The connection string
pointed to a nonexistent database
• Entity Framework
created a database
automatically
• Movies.mdf in the
App_Data folder
• View: Solution Explorer
| Show all files ,
Refresh, then expand
the App_Data folder
The Movies Table
• Double-click
Movies.mdf.
• This opens the
Database Explorer.
• Expand Tables to
see the Movies
table.
Right-click
Click
Movies Table Data
View the Movies Table Structure
• Right click the Movies Table
• Select Open Table Definition
• The structure was created by
Entity Framework
Right click
Click
The Movies Table Structure
Close the connection to the database
Right click on
MovieDBContext
Select
Close
Status …
• Thus far, we have
– Created a database
– Created a listing page
• Next
– Examine the scaffolded code
– Add a SearchIndex method
– Add a SearchIndex View
– Search for Movies
Download