Accessing your Model’s Data from a Controller
This is a beginner tutorial that introduces the basics of ASP.NET MVC. You’ll create a simple web application that reads and writes from a database. Visit the ASP.NET MVC learning center to find other ASP.NET MVC tutorials and samples.
In this section we are going to create a new MoviesController class, and write some code that retrieves our Movie data and displays it back to the browser using a View template.
Right click on the Controllers folder and make a new MoviesController.
This will create a new “MoviesController.cs” file underneath our folder within our project. Let’s update the MovieController to retrieve the list of movies from our newly populated database.
[!code-csharpMain]
1: using System;
2: using System.Linq;
3: using System.Web.Mvc;
4: using Movies.Models;
5:
6: namespace Movies.Controllers
7: {
8: public class MoviesController : Controller
9: {
10: MoviesEntities db = new MoviesEntities();
11:
12: public ActionResult Index()
13: {
14: var movies = from m in db.Movies
15: where m.ReleaseDate > new DateTime(1984, 6, 1)
16: select m;
17:
18: return View(movies.ToList());
19:
20: }
21: }
22: }
We are performing a LINQ query so that we only retrieve movies released after the summer of 1984. We’ll need a View template to render this list of movies back, so right-click in the method and select Add View to create it.
Within the Add View dialog we’ll indicate that we are passing a List<Movies.Models.Movie> to our View template. Unlike the previous times we used the Add View dialog and chose to create an “Empty” template, this time we’ll indicate that we want Visual Studio to automatically “scaffold” a view template for us with some default content. We’ll do this by selecting the “List” item within the “View content dropdown menu.
Remember, when you have a created a new class you’ll need to compile your application for it to show up in the Add View Dialog.
Click add and the system will automatically generate the code for a View for us that displays our list of movies. This is a good time to change the <h2> heading to something like “My Movie List” like we did earlier with the Hello World view.
Run your application and visit /Movies in the address bar. Now we’ve retrieved data from the database using a basic query inside the Controller and returned the data to a View that knows about Movies. That View then spins through the list of Movies and creates a table of data for us.
We won’t be implementing Edit, Details and Delete functionality with this application - so we don’t need the default links that the scaffold template created for us. Open up the /Movies/Index.aspx file and remove them.
Here is the source code for what our updated View template should look like once we make these changes:
[!code-aspxMain]
1: <%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<IEnumerable<Movies.Models.Movie>>" %>
2:
3: <asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
4: Movie List
5: </asp:Content>
6: <asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
7: <h2>My Movie List</h2>
8: <table>
9: <tr>
10: <th>Title</th>
11: <th>ReleaseDate</th>
12: <th>Genre</th>
13: <th>Rating</th>
14: <th>Price</th>
15: </tr>
16: <% foreach (var item in Model)
17: { %>
18: <tr>
19: <td><%: item.Title %></td>
20: <td><%: String.Format("{0:g}", item.ReleaseDate) %></td>
21: <td><%: item.Genre %></td>
22: <td><%: item.Rating %></td>
23: <td><%: String.Format("{0:F}", item.Price) %></td>
24: </tr>
25: <% } %>
26: </table>
27: <p>
28: <%: Html.ActionLink("Create New", "Create") %>
29: </p>
30: </asp:Content>
It’s creating links that we won’t need, so we’ll delete them for this example. We will keep our Create New link though, as that’s next! Here’s what our app looks like with that column removed.
We now have a simple listing of our movie data. However, if we click the “Create New” link, we’ll get an error as it’s not hooked up! Let’s implement a Create Action method and enable a user to enter new movies in our database.
|