ASP. net mvc 3 (accessing your model's data from a controller) (5/9)

Source: Internet
Author: User
Tags sql server express actionlink
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 newMoviescontrollerClass and write code that retrieves the movie data and displays it in the browser using a view template.

Right-clickControllersFolder and make a newMoviescontrollerClass.

This creates a newMoviescontroller. CSFile in the project'sControllersFolder. Let's updateIndexAction Method inMoviescontrollerClass so that is retrieves the list of movies. Don't forget to includeUsingStatement forMvcmovie. ModelsNamespace.

 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 passingMovieClass 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 ofIndexView 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 forPriceProperty 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 toMoviesController 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 ourMovieClass. In the next tutorial, we'll addCreateMethod to add movies to this database.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.