3. Introduction
ï” ASP.Net MVC is a framework for building web applications.
ï” Generally applies MVC Pattern on ASP.Net framework.
ï” Microsoft combines the effectiveness of model-view-controller.
4. Understanding the History of ASP.Net
ï” ASP.Net was a huge shift when it first arrived in 2002. Figure illustrate
Microsoftâs technology stacks as it appeared then.
5. Understanding ASP.Net Web Forms
ï” With Web Forms, Microsoft attempted to hide both HTTP and HTML by
modeling the user interface(UI) as a hierarchy of server side control
objects.
ï” Each control kept track of its own state across request, rendering itself as
HTML when needed and automatically connecting client-side events (for
example, a button click) with the corresponding server side event handler
code.
ï” Web Forms is a giant abstraction layer designed to deliver a classic event-
driven graphical user interface (FUI) over the web.
6. What to choose ?
Web Forms or MVC
ï” Two important factors you should consider while making the choice is :
ï” Rapid Application Development - If you want to develop anything rapidly
ASP.NET Web Forms is the only chance you are having, you canât even
consider for ASP.NET MVC for RAD.
ï” Unit Testing - If automatic unit testing is most important factor for you
MVC will be best for you.
ï” Other than what you can do is, write down all your project requirement
and try to compare them with Pros and Cons of both Web Forms and MVC
7. The MVC Pattern
ï” The MVC separates the user interface (UI) into three main aspects:
ï” The Model : A set of classes that describes the data youâre working with.
ï” The View : Defines how to applicationâs UI will be displayed.
ï” The Controller : A set of classes that handles communication from the user.
An application specific logic.
8. Creating an ASP.Net MVC Application
ï” Begin with choosing File - > New Project as in figure :
9. Creating an ASP.Net MVC Application
contdâŠ
ï” Select Visual C# - > Web Template List then select ASP.Net MVC 4
Application, Name your application and click ok as in figure :
10. Creating an ASP.Net MVC Application
contdâŠ
ï” After creating a new MVC 4 application,
youâll be presented with an
intermediate dialog with some MVC-specific
options for how the project should be
created, as shown .
ï§ Select internet application from this dialog box
ï§ select Razor View Engine
ï§ and if you want unit testing
check the checkbox of
Create a unit test project and then click Ok.
11. MVC Application Structure
ï” When you create a new ASP.Net MVC application with Visual Studio, it
automatically adds some files and directories to the project as shown :
12. The Models
ï” When you create a new ASP.Net MVC template application project it
contains a folder named Model.
ï” Model folder usually contains files with extensions .cs .
ï” Model folder contains classes. You yourself specify classes according to
what is your working domain. for example
13. The Models contdâŠ
ï” The MVC Model contains all application logic.
ï” In MVC models both hold and manipulate application data.
14. Adding classes to Models
ï” In the Solution Explorer, right click the Models folder.
ï” Select Add and Select Class.
ï” Name the class and click ok.
ï” A new class will be added to Models folder.
16. The Controllerâs Role
ï” Controller within the MVC pattern are responsible for responding to user
input.
ï” Controllers in the MVC pattern are concerned with the flow of the
application , working with data coming in, and providing data going out.
ï” With MVC the URL tells the routing mechanism which controller class to
instantiate and which action method to call, and supplies the required
arguments to that method
20. Writing Action Method
ï” Add as many Action Methods as you want:
ï” This is simple demonstration of writing Action Methods:
ï” When you create a new controller an index() Action Method already exist
with ActionResult signature change it to string to return a string and write
return âHello from Store.Index();â as follows :
21. Accessing Action Methods
ï” Users interact with controllers and handle users input :
ï” When you run the project browser opens and have URL like
http://localhost 24461/ControllerName/ActionMethod/Parameter Value
ï” Type ControllerName and Action Method in it which user request the
controller respond accordingly
ï” http://localhost 24461/Store/detail/ and press enter
22. The Purpose of Views
ï” The View is responsible for providing the user interface (UI) to the user.
ï” The view transforms that model into a format ready to be presented to the
user.
ï” In ASP.Net MVC, the view accomplishes this by examining a model object
handed off to it by the controller and transforming the contents of that to
HTML.
ï” By convention the views directory contains a folder per controller. With the
same name as the controller.
24. ViewData and ViewBag
ï” These both are use to pass data from controller to view.
ï” Technically, data is passed from the controllers to the views via a
ViewDataDictionary called ViewData.
ï” ViewData Syntax as follows :
ï” ViewBag Syntax as follows :
25. Adding A View
ï” The easiest way to display the Add View dialog is to right-click in an action
method. As follows
26. Adding A View contdâŠ
ï” This bring up the Add View dialog,
as shown :
27. Partial View
ï” An action method can also return a partial view in the form of a
PartialViewResult via the PartialView method.
ï” The partial view itself looks much like a normal view, except it doesnât
specify a layout.
ï” This is useful in partial update scenarios using AJAX.