ArticleDirectory
- Accessing your model's data from a controller
- Creating a connection string and working with SQL Server Express
Accessing your model's data from a controller
In this section, you'll create a newMoviescontroller
Class and write code that retrieves the movie data and displays it in the browser using a view template.
Right-clickControllersFolder and make a newMoviescontroller
Class.
This creates a newMoviescontroller. CSFile in the project'sControllersFolder. Let's updateIndex
Action Method inMoviescontroller
Class so that is retrieves the list of movies. Don't forget to includeUsing
Statement forMvcmovie. Models
Namespace.
Using System ; Using System . LINQ ; Using System . Web . MVC ; Using Mvcmovie . Models ; // Required in order to access moviedbcontext. Namespace Mvcmovie . Controllers { Public Class Moviescontroller : Controller { Moviedbcontext DB = New Moviedbcontext (); Public Actionresult Index () { VaR Movies = From M In DB . Movies Where M . Releasedate > New Datetime ( 1984 , 6 , 1 ) Select M ; Return View ( Movies . Tolist ()); } } }
The code is refreshing a LINQ query to retrieve only movies that were released after the summer of 1984. we'll need a view template to render this list of movies, so right-click inside the method and selectAdd ViewTo create it.
InAdd ViewDialog box, we'll indicate that we're re passingMovie
Class to the view template. Unlike the previous times when we usedAdd ViewDialog Box and chose to create an empty template, this time we'll indicate that we want visual web developer to automatically "scaffold" A View template for us that contains some default content. to do this, selectListInScaffold TemplateDrop-down list.
Remember that after you 've created a new class, you'll need to compile your application before the class shows up inAdd ViewDialog box.
click Add . visual Web Developer automatically generates the code for a view that displays our list of movies. this is a good time to change the
heading to something like" My movie list "like you did earlier with the" Hello World "view.
The code below show a portion ofIndex
View for the movie controller. In this section, change the format string for the release date from{0: g}
To{0: d}
(That is, from General date to short date). Change the format string forPrice
Property from{0: f}
To{0: c}
(From float to currency ).
In addition, in the table header, change the column name from "releasedate" to "release date" (two words ).
<Table> <Tr> <TH> </Th> <TH> Title </Th> <TH> Release Date </Th> <TH> Genre </Th> <TH> Price </Th> <TH> Rating </Th> </Tr> @ Foreach (VAR item in Model ){ <Tr> <TD> @ HTML. actionlink ("edit", "edit", new {id = item. id}) | @ HTML. actionlink ("details", "details", new {id = item. id}) | @ HTML. actionlink ("delete", "delete", new {id = item. id }) </TD> <TD> @ Item. Title </TD> <TD> @ String. Format ("{0: d}", item. releasedate) </TD> <TD> @ Item. Genre </TD> <TD> @ String. Format ("{0: c}", item. Price) </TD> </Tr> } </Table>
We have not added a connection string toWeb. configFile. when no connection string is provided, the code-first approach creates the database file in the default SQL express directory and gives the file a name based on the database context name with the namespace prepended. let's set an explicit connection string now.
Creating a connection string and working with SQL Server Express
Open the application rootWeb. configFile. (notWeb. configFile inViewsFolder.) The image below show bothWeb. configFiles; openWeb. configFile circled in red.
Add the following connection string to<Connectionstrings>
Element inWeb. configFile.
<Add Name="Moviedbcontext"Connectionstring="Server =./sqlexpress; database = movies; trusted_connection = true"Providername="System. Data. sqlclient" />
Run the application and browse toMovies
Controller by appending/MoviesTo the URL in the address bar of your browser. An empty list of movies is displayed.
A SQL Server express 2008 database calledMoviesWas automatically created for you. You can verify that it's been created by looking inC:/program files/Microsoft SQL Server/mssql10.sqlexpress/MSSQL/DataFolder.
Under the hood, our code-first approach has created an empty movies database with a movie table with columns that map to ourMovie
Class. In the next tutorial, we'll addCreate
Method to add movies to this database.