Skip to Main Content
ASP.NET 4 24-Hour Trainer
book

ASP.NET 4 24-Hour Trainer

by Toi B. Wright
July 2010
Beginner content levelBeginner
552 pages
10h 14m
English
Wrox
Content preview from ASP.NET 4 24-Hour Trainer

Chapter 19B. Displaying Data in MVC

Most web applications display data from a database. In this lesson I show you how to use the DataLayer that you created in the previous lesson to display data in an ASP.NET MVC application.

REFERRING TO THE DATALAYER

To use the DataLayer from the previous lesson you must first add a reference to the assembly. You can do this a couple of ways:

  1. Add the DataLayer project to the current solution and add a reference to the DataLayer project.

  2. Add a reference directly to the DataLayer assembly (DLL).

Either way, you need to add the connection string to the web.config file. This is the connection string from Lesson 18:

<add name="RecipeDBEntities" connectionString=
    "metadata=res://*/DataModel.csdl|res:
//*/DataModel.ssdl|res://*/DataModel.msl;provider=System.Data.SqlClient;provider
connection string="DataSource=.\SQLEXPRESS;
AttachDbFilename=C:\ASPNETTrainer\DataLayer\Recipes.mdf;IntegratedSecurity=True;
Connect Timeout=30;User Instance=True;MultipleActiveResultSets=True&quot;"
providerName="System.Data.EntityClient" />

This is the reference to the Entity Data Model that I use throughout this lesson:

RecipeDBEntities _entity = new RecipeDBEntities();

USING VIEWDATA

The SelectList class represents a list from which the user can select an item. In this case, the CategoryList will be used to populate a drop-down list. This is a Select action method that reads all of the records in the Category table, sorts them by Name, and returns them to the Select view using the ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

ASP.NET 4 24-Hour Trainer

ASP.NET 4 24-Hour Trainer

Toi B. Wright
ASP.NET 4 Unleashed

ASP.NET 4 Unleashed

Stephen Walther, Kevin Hoffman, Nate Dudek

Publisher Resources

ISBN: 9780470596913Purchase bookExamplesErrata