James Ray Anderson

James Ray Anderson
James Ray Anderson
0 comments

MVC - Create a .NET MVC Project in under 10 minutes

8:00 PM
Overview
This post is designed for those are familiar with creating ASP.NET C# Webforms and want to move into the ASP.NET C# MVC 4 projects.  While there are tons of tutorials out there that show you how to create projects - this is designed to be simpler and is meant to go an overall understanding. 

What You Will Need
Get Visual Studio 2012 and install it.

What You Will Do
The features of Visual Studio 2012 make it really easy to create an MVC application if you know what your underlying data is before you start.  It is basically a three-step process. 
  1. Create Project.  You will create the project. Simple enough.
  2. Create Model.  You will create the model, define data properties, and let Visual Studio create a database for you.
  3. Create Controller.  You will create a controller that accesses the data model and automatically creates the corresponding CRUD views for you.
NOTE:  For the sake of brevity, I will leave off all of the individual dialog settings unless a value other than default is needed. 

Create a Project
This is simple enough.  Just use the File > New > Project feature and select the appropriate application type.
  1. Create Project.  Create an ASP.NET MVC 4 Web Application.  Call it MyCompanies.
  2. Save. 
Create a Model
The model will use the Entity Framework for data access.  This is a Code First approach where you create the data model by writing classes and then have the database created on the fly.  It is easy enough to add new fields and update the corresponding views - but that is another topic.
  1. Add a Model.  Right-click on the Models folder and add a class.  Call it Company if you want.
  2. Define Model.  Define the data by adding properties to the class. For example:

    public int ID { get; set; }
    public string Title { get; set; }
    public DateTime PostDate { get; set
    ; }
     
     
  3. Data Entity.  Add a using statement to the class for System.Data.Entity.  For example:

    using System.Data.Entity;
     
     
  4. Define Data Context.  Add a class to the model called CompanyDBContext: DbContext with a public property for DbSet MyData { get; set;}.  For example:

    public class CompanyDBContext : DbContext
    {
          public DbSet<Company> Companies { get; set; }
    }
       
     
  5. Add Connection String.  Go to web.config at root and add a connection string.
     <add name="CompanyDBContext" connectionString="Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\MyCompanies.mdf;Integrated Security=True" providerName="System.Data.SqlClient"/>
      
     
  6. Save.  Save and Build.   
    
Create Controller
The controller will retrieve the data from the model and display it in a browser as a view.  The built-in features of VS and .NET will automatically create views corresponding to CRUD.  How sweet is that!?!
  1. Add a Controller.  Right-click on Controllers folder and select Add Controller
  2. Select the Model Class from the model you created, such as "Company".
  3. Select the DBContext you created, such as "CompanyDBContext".
  4. Click Add.

Check out Views
Expand the Views folder and go to the name of the class you created,  Expand that.  Notice that Create, Delete, Details, Edit, and Index are created for you.

Debug It
If you did this without any typos then hit F5.  Your browser will come up. 
  1. Type in the name of the Controller you created.  Just append the name of the controller to your URL.  For example:

    http://localhost:13026/company
     
  2. Your controller should automatically go to the Index.cshtml page for you.
  3. Voila.
  4. Click Create New.
  5. Add some data.
  6. Click Create.
  7. Double Voila.
More Tutorials
There are tons of tutorials, YouTube video, and what not.  Here are a couple you should at least visit:

0 comments:

 
Toggle Footer
Top