Working in Asp.Net MVC 3.0 in comparison to Web Forms

22
Working in Asp.Net MVC 3.0 in comparison to Web Forms http://kb.vteamslabs.com 1 By: Shahid Maqsood

description

Working in Asp.Net MVC 3.0 in comparison to Web Forms. By: Shahid Maqsood. Session Agenda. Overview of previous session Working of Controller, Model, View Model and Views Brief Introduction to Entity Framework. - PowerPoint PPT Presentation

Transcript of Working in Asp.Net MVC 3.0 in comparison to Web Forms

Page 1: Working in  Asp.Net  MVC 3.0 in comparison to Web Forms

Working in Asp.Net MVC 3.0 in comparison to Web Forms

http://kb.vteamslabs.com 1

By: Shahid Maqsood

Page 2: Working in  Asp.Net  MVC 3.0 in comparison to Web Forms

Session Agenda• Overview of previous session• Working of Controller, Model, View Model and Views• Brief Introduction to Entity Framework.• How following functionalities of Asp.Net are

used/implemented in MVC environment.– Master Page – Web Controls – Validation Controls – User Controls– Events Handling– State Management

2http://kb.vteamslabs.com

Page 3: Working in  Asp.Net  MVC 3.0 in comparison to Web Forms

Controller

• With traditional web frameworks, incoming URLs are typically mapped to files on disk. – For example: "/Products.aspx" or "/Products.php“

• Web-based MVC frameworks map URLs to server code in a slightly different way. Instead of mapping incoming URLs to files, they map URLs to methods on classes. – Classes are called “Controllers” and its Methods are

called “Action Methods”.

http://kb.vteamslabs.com 3

Page 4: Working in  Asp.Net  MVC 3.0 in comparison to Web Forms

Controllers Responsibilities

• Controllers are responsible for– Locating the appropriate action method to call and

validating that it can be called– Getting the values to use as the action method's

arguments– Handling all errors that might occur during the

execution of the action method– Determining the Response to send back to the client

(display HTML, download a file, redirect to a different URL, etc.)

– Providing the default WebFormViewEngine

http://kb.vteamslabs.com 4

Page 5: Working in  Asp.Net  MVC 3.0 in comparison to Web Forms

Events Handling (Action Method)

• The controller defines action methods. – Action methods typically have a one-to-one

mapping with user interactions– Action methods return the ActionResult return type– There are two type of action behavior of Action

methods GET and POST. GET is the default behavior.

http://kb.vteamslabs.com 5

Page 6: Working in  Asp.Net  MVC 3.0 in comparison to Web Forms

ActionResult Return TypeAction Result Helper Method Description

ViewResult View Renders a view as a Web page.

PartialViewResult PartialView Renders a partial view, which defines a section of a view that can be rendered inside another view.

RedirectResult Redirect Redirects to another action method by using its URL.

ContentResult Content Returns a user-defined content type.

JsonResult Json Returns a serialized JSON object.

JavaScriptResult JavaScript Returns a script that can be executed on the client.

FileResult File Returns binary output to write to the response.

EmptyResult (None)Represents a return value that is used if the action method must return a null result (void).

http://kb.vteamslabs.com 6

Page 7: Working in  Asp.Net  MVC 3.0 in comparison to Web Forms

Model

• Models hold and manipulate data.• They are usually database entities which are

mapped on table fields.

http://kb.vteamslabs.com 7

Page 8: Working in  Asp.Net  MVC 3.0 in comparison to Web Forms

Entity Framework

• Entity Framework (EF) support is included in ASP.NET MVC 3 projects to query and update the database.

• EF is a flexible object relational mapping (ORM) data API that enables developers to query and update data stored in a database in an object-oriented way.

• It has two approaches I-e Model First and Code First.

http://kb.vteamslabs.com 8

Page 9: Working in  Asp.Net  MVC 3.0 in comparison to Web Forms

Entity Framework

• In Model-first approach, you run a wizard and select the tables for which you need to create entities. It will create all the classes for you.

• In Code-first approach, you create model objects b writing simple classes. And then it allows you to create your database on the fly from your classes.

http://kb.vteamslabs.com 9

Page 10: Working in  Asp.Net  MVC 3.0 in comparison to Web Forms

Views

• Views hold our UI templates• ViewEngine is used to render the contents• ViewBag is used to pass data from Controllers

to Views• ViewModel (@model) is used for strongly-

typed views.• Layout is used for common site elements

http://kb.vteamslabs.com 10

Page 11: Working in  Asp.Net  MVC 3.0 in comparison to Web Forms

Add View

• View name• View engine• Strongly typed view• Partial view• Layout or master

page

http://kb.vteamslabs.com 11

Page 12: Working in  Asp.Net  MVC 3.0 in comparison to Web Forms

View Engine

• Following are the major View Engines– Razor View Engine– ASPX View Engine– Spark View Engine– Nhaml View Engine– Custom View Engine (WebFormViewEngine)

http://kb.vteamslabs.com 12

Page 13: Working in  Asp.Net  MVC 3.0 in comparison to Web Forms

ViewModels

• ViewModels are strongly typed classes which are optimized for our specific view scenarios.

• For example, Sometimes we need to use more properties for managing the view but our domain model doesn’t allow them because they are mapped to database tables only. So, we employ a view model for this kind of scenarios.

http://kb.vteamslabs.com 13

Page 14: Working in  Asp.Net  MVC 3.0 in comparison to Web Forms

Master Page (Layout)

• Using a Layout for common site elements– Most websites have

content which is shared between many pages: navigation, footers, logo images, stylesheet references, etc.

– The Razor view engine makes this easy to manage using a page called _Layout.cshtml

http://kb.vteamslabs.com 14

Page 15: Working in  Asp.Net  MVC 3.0 in comparison to Web Forms

_Layout.cshtml File

http://kb.vteamslabs.com 15

Page 16: Working in  Asp.Net  MVC 3.0 in comparison to Web Forms

Web Controls (Html Helpers)

• In Web Forms, we have web controls in System.Web.UI I-e Textbox, Dropdownlist, Label, Checkbox, Radiobutton etc.

• In MVC, above controls are not available. MVC View Engine provides Html helpers for these controls.

• Eamples:• @Html.EditorFor(model => model.Title)

@Html.DropDownList("ArtistId", String.Empty)@Html.LabelFor(model => model.Price)

http://kb.vteamslabs.com 16

Page 17: Working in  Asp.Net  MVC 3.0 in comparison to Web Forms

Validation Controls (Data Annotations)

• In Web forms, we have validation controls to perform validations I-e RequiredFieldValidator, RegularExpressionValidator etc

• In MVC, we specify validation in model classes along with properties through Data Annotations. And then we use Html Helpers to specify the place of validation messages next to controls.

• Example:@Html.EditorFor(model => model.Title) @Html.ValidationMessageFor(model => model.Title)

http://kb.vteamslabs.com 17

Page 18: Working in  Asp.Net  MVC 3.0 in comparison to Web Forms

Data Annotations

http://kb.vteamslabs.com 18

Page 19: Working in  Asp.Net  MVC 3.0 in comparison to Web Forms

Web User Controls (Partial Views)

• In Web forms, we have Web User Controls for managing the repeating functionality which can be used on multiple pages.

• In MVC, the alternative is to use Partial Views. The Partial Views can be called inside the views or the layout (Master Page) views.

• Example:@Html.Partial("_LogOnPartial")

• Partial Views are added to Shared folder.

http://kb.vteamslabs.com 19

Page 20: Working in  Asp.Net  MVC 3.0 in comparison to Web Forms

State Management

• Should not use View State.• No Control View State.• ViewBag to communicate between Controller

and View• Sessions are available.

http://kb.vteamslabs.com 20

Page 22: Working in  Asp.Net  MVC 3.0 in comparison to Web Forms

Thank you!

Questions please?

http://kb.vteamslabs.com 22