MVC Basics Training Material
MVC Basics Training Material
Sensitivity: Internal
INTRODUCTION TO MVC
ASP.NET MVC
ASP.NET MVC is a web framework based on Model-View-Controller (MVC) architecture. We can build dynamic
web applications using ASP.NET MVC framework that enables a clean separation of concerns, fast development,
and TDD friendly.
ASP.NET MVC Versions
Sensitivity: Internal
Model: Model represents the shape of the data. A
class in C# is used to describe a model. Model
objects store data retrieved from the database
Sensitivity: Internal
MVC Lifecycle
Sensitivity: Internal
HOW TO CREATE MVC PROJECT IN VISUAL
STUDIO
Sensitivity: Internal
HOW TO CREATE MVC PROJECT IN VISUAL
STUDIO
Sample Model
Sensitivity: Internal
MVC – View Sample
Sample View
Sensitivity: Internal
MVC - View
1
Sensitivity: Internal
MVC – Controller Sample
Sample Controller
All the public methods in the Controller class are called Action methods.
Sensitivity: Internal
MVC – Routing
Routing
•Routing
1
Sensitivity: Internal
MVC – Route Config
1
Sensitivity: Internal
MVC - Routing
Multiple Routing
1
Sensitivity: Internal
MVC – Razor View
1
Sensitivity: Internal
MVC – HTML Helpers
1
Sensitivity: Internal
MVC – HTML Helper
1
Sensitivity: Internal
MVC _ HTML Helpers
1
Sensitivity: Internal
MVC - Validation
Data Annotation :
These attributes are used to define metadata for ASP.NET MVC and ASP.NET data controls.
We can apply these attributes to the properties of the model class to display appropriate validation messages to
the users.
1
Sensitivity: Internal
MSC - Data Annotation
1
Sensitivity: Internal
MVC – Layout view
ASP.NET MVC introduced a Layout view which contains these common UI portions so that we don't have to
write the same code in every page.
eg. header, left navigation bar, right bar, or footer section
For example, An application UI may contain a header, left menu bar, right bar, and footer section that remains the
same on every page. Only the center section changes dynamically, as shown below
The layout view allows you to define a common site template, which
can be inherited in multiple views to provide a consistent look and
feel in multiple pages of an application.
The layout view for the sample UI would contain a Header, Left
Menu, Right bar, and Footer sections. It has a placeholder for the
center section that changes dynamically.
1
Sensitivity: Internal
MVC - Layout
1
Sensitivity: Internal
MVC - Layout
1
Sensitivity: Internal
MVC - ViewStart
1
Sensitivity: Internal
MVC – Layout in Child view/Action method
Specify the layout view name as a second parameter in the View() method, as shown below. By default, layout view
will be searched in the Shared folder.
1
Sensitivity: Internal
MVC – Partial View
1
Sensitivity: Internal
MVC – Render Partial View
We can render the partial view in the parent view using the HTML
helper methods:
@html.Partial(), html.RenderPartial(), @html.RenderAction().
Html.Partial() Html.RenderPartial()
Html.Partial returns html string. Html.RenderPartial returns void.
Html.Partial injects the html string of the partial Html.RenderPartial writes html in the response
view into the main view stream.
1
Sensitivity: Internal
MVC – Render Action
1
Sensitivity: Internal
MVC – Action Method
Action Method :
All the public methods of the Controller class are called Action methods.
They are like any other normal methods with the following restrictions:
Action method must be public. It cannot be private or protected
Action method cannot be overloaded
Action method cannot be a static method.
1
Sensitivity: Internal
MVC – Action Result
Action Result :
MVC framework includes various Result classes, which can
be returned from an action method.
The result classes represent different types of responses,
such as HTML, file, string, JSON, javascript, etc
1
Sensitivity: Internal
MSC – Action Verbs
We can apply one or more action verbs to an action method to handle different HTTP
requests.
1
Sensitivity: Internal
MVC – Action Verbs
1
Sensitivity: Internal
MVC – Action Verbs
1
Sensitivity: Internal
MVC – View Bag
ViewBag :
ViewBag is used to transfer temporary data (which is not included in the model) from the controller to the view.
ViewBag doesn't require typecasting while retrieving values from it. This can throw a run-time exception if the
wrong method is used on the value.
ViewBag is a dynamic type and skips compile-time checking. So, ViewBag property names must match in
controller and view while writing it manually.
1
Sensitivity: Internal
MVC – View Data
View Data:
1
Sensitivity: Internal
MVC – Temp Data
TempData is used to transfer data from TempData stores the data temporarily for one
view to controller, controller to view, or request. If we want to retrieve data for next
from one action method to another action request we can use keep().
method of the same or a different
controller.
1
Sensitivity: Internal
MVC - Filter
controllers define action methods that usually have a one-to-one relationship with possible user
interactions, but sometimes you want to perform logic either before an action method is called or after
an action method runs
Filters are custom classes that provide both a declarative and programmatic means to add pre-action
and post-action behavior to controller action methods.
1
Sensitivity: Internal
MVC - Filter
Register Filters :
Filters can be applied at three levels
1
Sensitivity: Internal
MVC - Filter
1
Sensitivity: Internal
MVC - Filter
1
Sensitivity: Internal
MVC – Bundling & Minification
Bundling and minification techniques were introduced in MVC 4 to improve request load time.
Bundling allows us to load the bunch of static files from the server in a single HTTP request.
Minification technique optimizes script or CSS file size by removing unnecessary white space and comments
and shortening variable names to one character.
Bundling Minification
1
Sensitivity: Internal
MVC – Bundle Types
1
Sensitivity: Internal
MVC - Area
1
Sensitivity: Internal
MVC - Area
1
Sensitivity: Internal
MVC – Global.asax.cs
1
Sensitivity: Internal
MVC – App_Start
1
Sensitivity: Internal
MVC – Web.config
1
Sensitivity: Internal
Sensitivity: Internal