Watch videos with subtitles in your language, upload your videos, create your own subtitles! Click here to learn more on "how to Dotsub"

Creating a Movie Database Application in 15 minutes with ASP.NET MVC

0 (0 Likes / 0 Dislikes)
  • Embed Video

  • Embed normal player Copy to Clipboard
  • Embed a smaller player Copy to Clipboard
  • Advanced Embedding Options
  • Embed Video With Transcription

  • Embed with transcription beside video Copy to Clipboard
  • Embed with transcription below video Copy to Clipboard
  • Embed transcript

  • Embed transcript in:
    Copy to Clipboard
  • Invite a user to Dotsub
[Microsoft ASP.net] [www.ASP.net] Hi, my name is Stephen Walther, and in this video I'm going to blast through building an entire ASP.NET MVC application from start to finish. The goal here isn't to offer a lot of explanation; I just want to give you a quick overview of the process of building an MVC application, and I want to keep the whole process really short. So what we'll be doing is we'll be creating a basic database-driven application. We'll be creating a movie database application, and our movie database application will enable us to list records from a database... and it will allow us to insert new records in a database and that's it. So let's go ahead and get started. I'm going to use Visual Studio 2008 to create my MVC application. I'm going to start by going to File and New Project. Don't select New Web Site; select New Project. Then what I'm going to do is I'm going to pick Visual C# as my programming language. I'm going to pick the Web category over here on the left, and I'm going to pick ASP.NET MVC Web Application as my project template over here on the right. I'm going to name my new application MovieApp, click OK, and it's going to ask me if I want to create a test project. We're actually not going to create unit tests, but I always find it easier just to say yes here in case I need to create unit tests later on. So I'm going to say yes, I'm going to click OK and create a separate unit test project. At this point Visual Studio is creating 2 projects. It's creating our actual movie database application but it's also creating that unit test project. You'll see we have something called MovieApp and down here we have something called MovieApp Tests. When Visual Studio first creates a new MVC application, it gives us a number of sample files by default. I want to start from scratch, so I'm going to erase those. I'm going to go to my Controllers folder and I'm going to erase the HomeController. Remove that. And I'm going to go to my Views folder and I'm going to remove the Home folder. I've erased that. And down here in my Unit Tests I'm going to go ahead and remove the Unit Test for my Home Controller. All right. So now we're ready to go ahead and build our application. I'm going to start by creating a new database. I'm going to create a SQL Express database. I'm going to right click on the App_Data folder, I'm going to select Add, New Item, I'm going to select SQL Server Database, and I'm going to call my new database MoviesDB, click Add, and I have a new SQL Express database. Now I want to add a database table named Movies. I'm going to double click on the MoviesDB database, and that's going to open up the Server Explorer window for me. I'm going to right click on Tables, Add New Table, and I'll start with an Id column, an identity column. It'll be an integer column. I want to make sure that that's a neat column. So auto number as I add new. So I'm going to extend Identity Specification, double click next to Identity. This is now an identity column. Movies should have titles, so I'm going to add a Title column. And movies have a director, so let's put in a Director column. And finally, let's put in a DateReleased column, a datetime column, don't allow nulls. I'm going to click the floppy to save my new database table. I'm going to name it Movies. And now we have our necessary database objects. It will make life easier if we have some data to work with, so I'm going to go ahead and right click on the new table that we just created, select Show Table Data, and I'm just going to add a couple of movies into my database. I'm going to add Star Wars. Lucas was the director. I have no idea when it was released, so I'm just going to put in 1977. King Kong. That was directed by Jackson. And again, I really don't know when that was released. Let's say that that was released in 2001. All right. So now I have some data. I have a database. The next step for me is I need to be working with that database from my MVC application. To do that, I'm going to take advantage of the Entity Framework to create some data model classes. I'm going to go to my Models folder, I'm going to select Add, New Item, I'm going to select the Data category, and I'm going to select ADO.NET Entity Data Model. You do need Service Pack 1 to use the Entity Framework, so you do need ASP.NET 3.5 Service Pack 1 or, more generally, the .NET Framework Service Pack 1 to use the Entity Framework. I'm going to name my data model classes DataModel, click Add. This opens up the Entity Data Model Wizard. I want to generate my classes from the database. I'm going to pick my MoviesDB database. If that doesn't show up, you can pick it from the drop-down list. Notice the name is going to create an object context class called MoviesDBEntities. Click Next. This allows me to pick the database tables I want to create model classes for. I only have one table called Movies, so I'll select that. I'm going to change the namespace to Models and click Finish. At this point the Entity Framework Designer opens up and it shows the new Movies classes that we created. This is a C# class the Entity Framework has generated for us. It represents the movie database table. Notice it's called Movies. This is going to represent a particular row from the Movies database table, so I want to change this name. I'm going to click on it and change this name to Movie, and that way it makes more sense because it represents a particular movie. So now we've created our model class. If you look over here in the Solution Explorer, now our Models folder has a DataModel.edmx file, and that contains our model classes for our MVC project. The next thing I want to do is create a controller, so I'm going to right click on the Controllers folder, I'm going to select Add, Controller, and I'm going to name my controller HomeController. Notice there's a checkbox down here. I'm going to check it. What that will do is it will automatically add stub methods for common database operations— listing database records, creating them, showing details for them, and editing them. I'm going to click Add. I've gotten my HomeController. I'm going to close this window down here on the bottom. I want to start by showing all the database records from my Movies database table. So I'm going to create an instance of the Entities Framework context class. DBEntities. Notice the red underline. That tells me I can hit Control dot. I'm going to hit Return and that will add the right namespace into the page so I can use the MoviesDBEntities class. Now I have way to communicate with the database. I have an action called Index, so I'll now use that entities field I just created, type in MovieSet and convert it to a list, and now I'm returning all the movie records. Now I'm ready to create a view to actually display the records, so I'm going to right click on Index, I'm going to select Add View. It's called Index right here. I'm going to say I want to create a strongly-typed view. I'm not going to get any data classes right now. The reason for that is I haven't compiled my application. So I'm going to click Cancel and I'm going to do a build. There we go. No errors. That's always a good sign. I'm going to click Add View again and select Create a strongly-typed view. For the View data class now I can select Movie. For the View content I want to show the list of movies, so I'm going to select List. I'm going to click Add, and this auto-generates a view for listing all the movies. Notice that it auto-generates a view with an HTML table inside of it which shows all of the properties, the value of all the columns from the underlying database table. Now we can run our application, and we can actually see our database records. The first time the application is going to ask if I want to go into debug mode. OK. It will open up a browser. And notice that we get our database records. It actually is formatted nicely. It's using the default style sheet and it's using the default master page. The next step for us is we're now able to show the records; I want to be able to add a new record. I'm going to modify 2 other controller actions in the HomeController. Going back to the HomeController, I want to modify these 2 actions named Create. Notice there's 2 Create actions. The reason for that is the first Create action is called when you're retrieving the form for creating a new movie, and then that form submits this form data to the second Create action down here, which is actually responsible for taking that form data and inserting a new movie database record into the database. The first thing I want to do is I want to create the form for creating a new movie. I'm going to right click on this Create action and select Add View. It's going to be a strongly-typed view. I want it to represent a movie. But this time for the View content I'm going to select Create, click Add, and now I have a new HTML form. This is auto-generated for me and it has form fields. This is going to create text boxes for each of the columns in the database. Notice it has created a text box for the Id column. That's an auto number column, that's an identity column, so we don't want that, so I'm going to delete that field. Now we have a perfectly fine form for entering form data. Going back to the HomeController, I need to create the second Create action here. This is the action that actually submits the data to the database. I'm going to change this so it actually takes a movie as a parameter. I'm going to call it movieToCreate. When we have it create the movie, we don't want it to create the Id column because that's an auto-generated column. I'm going to go ahead and tell it to exclude that column... when creating this movieToCreate object. The next thing I need to do is fill out the database logic here. I'm going to put in entities.AddToMovieSet(movieToCreate); and then save the modified movie set by calling SaveChanges. And we should be done. I'll go ahead and run this. The default Index view includes a link for creating a new record, so I'm going to go ahead and click on that, and I'll put in a new movie. I just saw Australia. I have no idea who directed that. Somebody. And I also have no idea when it was released, so I'll just say sometime in 2008, click Create, and notice we get our new database record. We're done. We've created a basic database-driven application, a movie database application, using ASP.NET MVC. It's only taken us a few minutes here. The goal of this video was hopefully to give you a sense of the process of building an ASP.NET MVC application. [Microsoft ASP.net] [www.ASP.net]

Video Details

Duration: 12 minutes and 4 seconds
Country: United States
Language: English
License: All rights reserved
Genre: None
Views: 6
Posted by: neudesicasp on Sep 6, 2013

Stephen Walther builds an entire ASP.NET MVC application from start to finish. This video is a great introduction for people who are new to the ASP.NET MVC Framework and who want to get a sense of the process of building an ASP.NET MVC application.

Caption and Translate

    Sign In/Register for Dotsub to translate this video.