Skip to main content

Programming with ASP.NET MVC4 - Part 01

Getting start….

The verbal meaning of MVC is Model View Controller. Which is well tested architecture especially designed for web based solutions.

  • Model : classes that represent application data and business rules
  • View: template files application will able to use for dynamically generate html responses.
  • Controller: classes which are manage browser request, retrieve data from model and define the view template for browser response. 

Let’s start with simple application.

Creating project


First of all make sure you are going to use visual studio 11 or above version with c#.

Step 1:  Click on FILE ->New ->Project.

Step 2: Select Web under Visual c# ->ASP.NET MVC4 Web Application and name project as      “MyMVCMovieList” -> click OK button.



Step 3: Select Internet application and keep View engine as Razor -> Click on OK button.


Step 4: You will get following screen.

Above is kind of simple hello world application. Just click on debug button or press F5.You will get following screen,

Adding Controller


Step 1: Right click on “controller” folder on solution explorer -> Add -> Controller (names it Helloworld and click OK button).


You will see below screen. Following index method uses view template to generate an html response to the browser. Actually it returns result of processed data (Action result).

Adding View


Step 1: Go to the HelloworldController class and right click on inside Index method -> Click on “Add View”.


Then following dialog box will appear,


Step 2: Click on Add button (Index.cshtml file will create).


Step 3: in Index.cshtml under <h2> tag add following code




In solution go to Index.cshtml and right click on it and then you will see “View in Page Inspector” there you can learn more things about new technology tool.


Now debug the application (Press F5). You will see following screen,


In above image you can see “your logo here” sentence. It is appearing at every page, therefore it should be under shared view and it is about layout. Thus it means we can change it through _Layout.cshtml. There you will find above sentence and change it to “My Movie List”.


Now run and see the result as previous.


Change the title index to “My movie list”

Passing data from controller to view


Controller is the brain of this system and it is the one which responsible for handling browser request and passing processed data to the view. All c# code and database handling part is doing by controller and view never cannot perform business logic or to communicate with database directly.
Go to the HelloworldController add following code,


There viewBag is a dynamical object and it doesn’t have predefined variables. Therefore TheASP.NET MVC model binding system automatically maps the Message and NumTimes parameters. Then go to the Build menu -> click on Build “MyMVCMovieList”. Now you have prepared data to pass and need a view template for that. Let’s do it,

Add view for welcome method. Name it welcome. And add following code under <h2> tag.


Now run the application and browse below URL.



Adding Model


Model classes are used for manage database activities.

Step 1: Right click on “Models” folder on solution explorer -> Add -> New Item 

There select Class (under c#) and name it Movie.


Add following properties to Movie class,

public int ID {get;set;}
public string Title{get;set;}
public DateTime ReleaseDate{get;set;}
public string Genre{get;set;}
public decimal Price{get;set;}

In same file add below class also,

public class MovieDBContext:DbContext
  {

     public DbSet <Movie>Movies{get;set;}

  }

Above MovieDBContext class to fetching updating and storing data to database.


To connect with database need to set connection string. To do that goes to “Web. Config” file in bottom of solution explorer. There you will able to set connection string.


Below is a sample connection string

<connectionStrings>
<add name="ABCDb" connectionString="data source= 192.168.10.1\server;Database=Sampledb; persist security info=True;User ID=sa; Password=@123";providerName="System.Data.Sqlent" />  
</connectionStrings>



Comments

Popular posts from this blog

Accessibility Testing

Accessibility Testing is a non-functional testing type. This means anyone can use a particular web application, mobile application, website, or any other software package. Any human means not only perfect men or women but also differently able people as well. Things can be used for testing, 

Differences between Severity ,Priority and Status

In market there are many bug tracking and project management tools available. These tools allow us to keep essential information of a bug or task. You will see there are three places have to fill when key in a task. Those three places are Status, Priority and Status of a bug/task. Let's see what those stand for.  Severity Priority Status Connotation Severity of a bug means how much it affecting to functionality of the software. Total damage it can do. Priority means how fast a bug or task should be resolve. Status means the current position of the bug/task. Types            Critical            Major            Moderate            Minor           Cosmetic           Low           Medium           High           New           Active/Open           Resolved/Fixed           QA In progress           Reopen           Closed/Completed 

MVC Architecture

Model-View-Controller (MVC) is a design pattern recently most popular among software engineers for applications which are needed to provide multiple views with using same data. I am suggesting MVC is better than WCF, because of this module separation. For an examples       ·          Multiple views and controllers can interface with same model for modification even those never exist before add this model.       ·          It is supporting all type of HTML verbs.       ·          No view state and post back events.       ·          Easy integration with JavaScript framework.       ·          Helps to decrease complexity of project       ·          Provides RAD development ( http://en.wikipedia.org/wiki/Rapid_application_development ).