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
Post a Comment