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

5 Minute Introduction to 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] [] [Scott Hanselman] Hi, I'm Scott Hanselman. Let' talk about MVC and see what we can accomplish in a few minutes. We'll use Visual Studio Express. It's a free integrated environment that makes development easy and enjoyable. Let's create a new project. Well select MVC 3. We'll call our project "Sports." From the new project dialogue I'll select that I want to make an Internet application using the new Razor view engine, and I'll also want my project to use HTML5 semantic markup. I'll click "okay" and create my default application. Let's run the default application and see what we've got. Here's our basic MVC application. Notice when I click on a "About" my URL says /Home/About. /Home/About maps directly to the home controller and the about method. I could create another controller. I'll call it the junk controller and hit add. If I run this and type in /Junk I'm told that I don't have a view for my junk controller. I'll right click within my code and say add view. We'll create a default empty view: "Hi, I'm the junk controller's index view." and we'll run this application. When I type in /Junk we're in the junk controller's index view, but we don't have a model. Let's create something a little it more sophisticated and we'll use the Entity Framework Code First to create our database. In the models folder let's add a new class; my class "Team" for now. We'll have teams, and teams have players. So our public class team will have a property, TeamId. I'll use snippets to make the creation of my properties even easier. Prop, tab, tab, string, tab, tab, Name, enter. Prop, tab, tab, string, tab, tab, City. Prop, tab, tab, DateTime and let's give a Founded for the date that my team was founded. I can even add little annotations like "Required" from the data annotations namespace to indicate that my team has to have a name. Let's say that teams also have players. Let's create a public class, tab, tab, Player. Players have player IDs, names, but they're going to need a reference to a TeamId because a player is on a certain team. That TeamId using Entity Framework Code First is going to help us relate the two classes together and also help Entity Framework Code First create the database. When we access our team from code we'll use this virtual property. Since a player can have a team then a team can have a collection of players—also a virtual property that we'll call Players. Now we've created the model for our application but note that we have no database. We have SQL Server Express installed and Code First will, by default, use SQL Server Express to hold our data. We made a junk controller, but it's going to take a bit of work to create a controller for teams and players. I'll use the new scaffolding that's built into MVC 3. I'll say add controller. We'll make a team controller using a template that will create read/write actions and views using the Entity Framework model that we created earlier. We'll point it at the team model and create a new data context called the SportsContext. I'll hit "okay" and press "add" and notice in the solution explorer a lot of code is being generated like a SportsContext class. This class holds the teams and players for Entity Framework. We'll access this class from our controller as well as a number of things that are also generated, including create, delete, details, edit, and a list in our index view, and the scaffolding system has created new controller actions for us for listing our teams, getting team details by ID, create, edit, and delete. Let's make a controller now for the players. Add controller. We'll call it the Player controller. We'll make a new complete CRUD—create, read, update, and delete—system. We'll use the Player model and the existing data context called SportsContext. Again, we'll hit "add." Notice the new controller is created as well as create, read, update, and delete—all in Razor. We've got controllers and views generated from that model we created earlier. Let's run this and see what we've got. We'll start by visiting the team controller. At this point a database has been created using SQL Server Express. We could also use SQL Compact Edition for a nice, fast, Xcopy-deployable, in-process database. Let's create a new team and maybe another team. I've got edit, details, and delete for my teams. Let's visit our player controller and create a new player. Notice the drop down The information we gave our system when we created our Entity Framework Code First model associated teams and players, and the scaffolding system realized that players had a relationship with teams and automatically created a relationship drop down. We can see here the names of players and the teams that they're on. Let's switch back over to Visual Web Developer and look inside the database explorer. This database was automatically generated using this model. I can open up the players, see the players that were just entered in the database. Their team IDs appear here, but the teams are referenced inside of our application. So in just a few minutes you've seen how easy it is to create an application using MVC. The ability to create a model using code only, automatic creation of a database, scaffolding features that enable quick creation of controllers, as well as the fun and easy to use Razor syntax will enable you to create dynamic websites very fast and still maintain a clean separation of concerns between your models, views, and controllers. I hope you enjoyed this brief overview of MVC 3 and I encourage you to check out the rest of our site. We've got lots of tutorials, videos, and online books for you to learn more about MVC. Thanks. Have fun. [Microsoft] []

Video Details

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

Scott Hanselman presents a 5 minute introduction to ASP.NET MVC.

Caption and Translate

    Sign In/Register for Dotsub to translate this video.