MVC 5

CHAPTER-1

Web Applications:-


Web Applications are programs that are executed on a web server and accessed from a web browser.


Three Layers of a web application:-

  1. Presentation Layer
  2. Business Logic Layer
  3. Data Layer

Presentation Layer:-

Consists of the interface through which the users interact with the application.

Business Logic Layer:-

Consists of the components of the application that control the flow of execution and communication between the presentation layer and the data layer.

Data Layer:-

Consists of the components that expose the application data stored in databases to the business logic layer.

Architecture of a web Application:-

  1. Single Tier
  2. Two Tier
  3. Three Tier

Single Tier:-

In single tier architecture, all the three layers are integrated together and can be installed on a single computer. If the application needs to be accessed on multiple computers a separate installation required.

Two Tier:-

In two tier architecture, the three layers are distributed over two tiers, a client and a server. The presentation layer resides on each client computer, the business logic layer resides either on client or on server, and data access layer resides on the server.

Types of two tier architecture:-

1> Fat Client and thin server:-

The architecture in which the business logic layer resides on the client is known as the fat client and thin server.

2> Fat server and thin client:-

The architecture in which the business logic layer resides on the server is known as the fat server and thin client.


Three Tier architecture:-

In three tier architecture, the three layers of the application are placed as three different entities. This architecture is used for those applications in which merging the business logic layer with the presentation layer or the data layer may degrade the performance of the application.


MVC( Model View Controller):-

Model:-

Refers to a set of classes that describes the data that the applications work with.

View:-

Refers to the components that define an application user’s interface

Controller:-

Refers to a set of classes that handle communication from the user and the overall application flow. A controller responds to user input , communicates with the model, and decides the view to render.


Features of ASP.NET MVC:-

Routing:-

MVC has a powerful routing system. Routing is a feature that enables you to develop applications with comprehensible and searchable URLs.

Scaffolding:-

MVC provides a scaffolding feature that provides a quick way to generate the code for commonly used operations in a standardized way.

Convention over Configuration:-

Asp.Net MVC Supports Convention over Configuration, which is a software design paradigm that aims at reducing the number of decisions taken by developers.

Bundling and Minification:-

Bundling is a technique provided by MVC that allows you to combine multiple files , such as CSS and javascript into a single file. Bundling improves the request load time by reducing the number of requests to the server.
Another Technique minification removes  unnecessary white space and comments from javascript or CSS Code, and shorten variable names.


Structure of an ASP.NET MVC Projects:-








Chapter 2

Working With Controllers and Views


What is Controller?

Controllers within an MVC Application are responsible for processing incoming user requests, executing the appropriate application code, communicate with the model, and rendering the required view. In other words controller manage the flow of application. Controllers are implemented in an MVC application as C# classes inherited from System.Web.Mvc.Controller class. A controller class contains an application logic in the form of various public methods called action methods.


Creating a Controller:-

Steps:-
  1.  Open Visual Studio and select Asp.Net Web Application. A popup window show you the list of templates where you can select  “Empty”, “Web Forms”, “MVC”, “Web API” etc..
  2. Select MVC and click on OK. Then it will take some time to load your project.
  3. After loading the project go to solution explorer and you have the options like below screen shot.
  4. To create a controller goto the Controllers folder given in the solution explorer.
  5. Right click on the controller and goto add then select controller.
  6. A new window will appear where you have three options if you use Visual Studio 2015.
  7. You need to choose MVC5 Controller-Empty.
  8. Then click on Add.
  9. Give the name of your controller then click on Add button. It will add controller in Controllers folder.

Create Action Methods:-

An Application needs to handle user interactions, such as entering a url in the web browser, clicking a hyperlink and submitting a web form. Each of these interaction sends a request to the web server. In ASP.NET MVC such requests are handled by public methods in the controllers. These public methods are called action methods. You can define these action methods in the controller class.





How to Pass parameters in controller actions:-







Working with Views:-

A view is a combination of HTML markup and code that runs on the web server. The code is embedded in the web pages using razor syntax. A web application contains multiple views, where each view is responsible for  either displaying some information to users or taking input from users.

To add view right click on Action Method Name and select Add view option.

Passing Data from a controller to a View:-
Two techniques to pass data from a controller to a view:-
  1. ViewData
  2. ViewBag


ViewData:-
ViewData is a dictionary of objects derived from the System.Web.Mvc.ViewDataDictionary class. It enables you to pass the data between a controller and a view. Here, values can be set using key/values pairs in the controller action method.

ViewBag:-
ViewBag is a dynamic object that allows passing data between a controller and a view. You can add properties of multiple types and their values by using the ViewBag object in a controller action method.


On Controller Action Method:-


On View:-


Output:-


Difference between ViewData and ViewBag and TempData:-
ViewData
  1. ViewData is used to pass data from controller to view
  2. It is derived from ViewDataDictionary class
  3. It is available for the current request only
  4. Requires typecasting for complex data type and checks for null values to avoid error
  5. If redirection occurs, then its value becomes null
ViewBag
  1. ViewBag is also used to pass data from the controller to the respective view
  2. ViewBag is a dynamic property that takes advantage of the new dynamic features in C# 4.0
  3. It is also available for the current request only
  4. If redirection occurs, then its value becomes null
  5. Doesn’t require typecasting for complex data type
TempData
  1. TempData is derived from TempDataDictionary class
  2. TempData is used to pass data from the current request to the next request
  3. It keeps the information for the time of an HTTP Request. This means only from one page to another. It helps to maintain the data when we move from one controller to another controller or from one action to another action
  4. It requires typecasting for complex data type and checks for null values to avoid error. Generally, it is used to store only one time messages like the error messages and validation messages



Defining Razor:-

Razor is a markup syntax that allows you to embed server side code (written in c# or VB) in an HTML markup. Razor is a clean, lightweight and simple view engine with a minimum amount of syntax and code. Razor allows embedding server side code in a view.

Razor Syntax Rules:-

  1. The @ character indicates a transition from markup to code.
  2. Razor code statements end with semicolon(;).
  3. The C# code is case sensitive.
  4. Variables should be declared by using the var keyword.
  5. Strings should be enclosed within quotation mark.

Partial View:-

A partial view is a part of the view that can be used on multiple pages. Partial views are crated in the views/shared folder. The name of the partial view is prefixed with an (_) character. You can render a partial view in a view using @Html.Partial() method.

Steps to create a partial view:-
  1. Goto views folder and click on it.
  2. Right click on the shared folder inside the views folder.
  3. Click on Add then choose view.
  4. Enter the name of the partial view with the prefixed underscore character. Then click on add. It will create a partial view.
  5. To render a partial view on a view use method @Html.Partial(“name of your partial view here”);




No comments:

Post a Comment

Insert, Update, Delete Data in Grid View Using Asp Panel on a single form Database:- create table StudentDetails ( Id int i...