Click here to Skip to main content
Click here to Skip to main content

How to ASP.NET MVC (Article 1)

, 14 Mar 2010 CPOL
Rate this:
Please Sign up or sign in to vote.
Develop a sample application using Asp.Net MVC


Download tables.zip - 700 B
Download MvcApplication1.zip - 307.28 KB


 


Introduction

Today I will discuss about how can we develop a simple Asp.Net MVC Application. As a first article about Asp.Net MVC i will today discuss about a very simple Application development. 

Background   

  MVC:

The Model View Controller (MVC) architectural pattern separates an application into three main components:

·                  Models: Model objects are the parts of the application that implement the domain logic. Often, model objects also retrieve and store model state in a database.

·                  Views: Views are the components that display the application's user interface (UI). Typically, this UI is created from the model data. An example would be an edit view of a Products table that displays text boxes, drop-down lists, and check boxes based on the current state of a Product object.

·                  Controllers: Controllers are the components that handle user interaction, manipulate the model, and ultimately select a view to render the UI. In an MVC application, the view only displays information; the controller handles and responds to user input and interaction.

 

ASP.NET MVC:

 

 The ASP.NET MVC framework is a lightweight, highly testable presentation framework that is integrated with existing ASP.NET features, such as master pages and membership-based authentication.

You must have the following items to develop asp.net mvc application

o         Microsoft Visual Studio 2008 SP1

o        Microsoft ASP.NET MVC 1.0 (visit www.asp.net for downloading )

o        Microsoft  SQL 2005 or Microsoft  SQL 2008 (Express edition or above)

o        Test database (Create a database named ‘Test’ and then run ‘test.sql’)

 

Database and Script:

    Before building/running this application please create a database named "Test" and run the "tables.sql" script.

Build a simple ASP.NET MVC Application


1.       File -> New -> Project -> Asp.Net MVC Web Application

project.jpg 

2        Allow with not select to create Unit test Project

2.jpg 

3.        Solution Explorere will be like that 

 3.jpg

 

Now I would like you to give a little description of the different folders use in Solution:

 

Content: Content hold the necessary Style sheet file

Controller:  Contains the Controller class. This is the entry point of any operation. For any user action, controller listen the action and process that action through ‘Model’ and finally return a ‘View’ associated with this action.

Script: Contains the related javascript used by the application

Views:  Views are returned by Controller. So for every controller like ‘HomeController’ there will be created a ‘Home’ in views and ‘AccoutController’ there will be created a ‘Account’ as well. There is also a folder in “Views” that is “Shared”. This “Shared” contains the necessary Master Pages and user controll.

 


That’s all the little description about the folder structure used by Asp.net Mvc.

 

4.       Right click the Model folder and add a new “Ado.net Entity DataModel”

 4.jpg

5.       Create Test.edmx with two tables Customer And Orders (Scripts is attached with source code)

5.jpg 

    Right click the “Controller” and Add a “Controller” named “CustomerController”. Check the “Add

      action methods for Create, Update, and Details scenarios” 

    6.jpg

 Now Modify CustomerController.cs like that:

 

 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Ajax;
using MvcApplication1.Models; 
namespace MvcApplication1.Controllers
{
    public class CustomerController : Controller
    {
        //
        // GET: /Customer/
        TestEntities entities = new TestEntities();

        public ActionResult Index()
        {

            return View(entities.Customer);
        }
        //
        // GET: /Customer/Details/5

        public ActionResult Info(string id)
        {
            var information = entities.Customer.Where(a => a.CustomerID == id).First();
            return View(information);
        }








 As today is our first Article so We will work only Selecting a list of customers with details. There have to need a study about the internal functionalities and architecture. So if you want ot know details please visit www.asp.net. Here i have used the TestEntities class. The different methods work actually how you want to interact with Model and finally you will get a View() like ActionResult Index() will return a Customer entity which will be the index page of Customer View.

public ActionResult Info() will return a perticular Customer Info using a ID

 

 Now 7. Right click inside the "public ActionResult Index()" method and Add View  selecting the check box "create a strongly-typed view", and select "Data Class" as "Customer" and "View Contenet" as "List"

       8. You will see a page "Index.aspx" has been added to "Views\Customer\"

       9. Right click also inside the "public ActionResult Info(string id)" method and add View  selecting the check box "create a strongly-typed view", and select "Data Class" as "Customer" and "View Contenet" as "Details" 

Thats all. You will get all the customer in Index.aspx page with Details Information. You need just a little adjustment in Index.aspx and Info.aspx page as "Create New" will not work as we did not use the " public ActionResult Create()" method. So modify the Page as i did in source code.

 

When will run the application, you will get all the customer information and when you will click the Details link, you will find details information about that customer.

 


Points of Interest  

Did you learn anything interesting/fun/annoying while writing the code? Did you do anything particularly clever or wild or zany?

History

Keep a running update of any changes or improvements you've made here.  

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

arif_uap
Database Developer Metatude-asia Ltd
Bangladesh Bangladesh
Hi
 
I am a Microsoft certified Technology specialist (.net framework 2.0 web)
I like to do programming in Microsoft Platform.
My Favorite language is C# and VB.Net

Comments and Discussions

 
GeneralMy vote of 1 Pinmembersunilkgupta20-Mar-12 3:24 
GeneralMy vote of 1 PinmemberRanjan.D17-Mar-10 23:17 
General[My vote of 1] Not enough detail PinmvpMark Nischalke15-Mar-10 4:14 
GeneralMy vote of 1 PinmemberJuraj Suchan15-Mar-10 0:58 
GeneralMy vote of 1 Pinmemberparagme28-Feb-10 20:43 
GeneralRe: My vote of 1 [modified] PinmemberJuraj Suchan15-Mar-10 0:58 
GeneralMy vote of 1 PinmemberSeishin#27-Feb-10 14:36 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.141223.1 | Last Updated 15 Mar 2010
Article Copyright 2010 by arif_uap
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid