220 likes | 429 Views
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.
E N D
Working in Asp.Net MVC 3.0 in comparison to Web Forms • By: ShahidMaqsood http://kb.vteamslabs.com
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 http://kb.vteamslabs.com
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
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
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
ActionResult Return Type http://kb.vteamslabs.com
Model • Models hold and manipulate data. • They are usually database entities which are mapped on table fields. http://kb.vteamslabs.com
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
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
Views • Views hold our UI templates • ViewEngineis used to render the contents • ViewBagis 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
Add View • View name • View engine • Strongly typed view • Partial view • Layout or master page http://kb.vteamslabs.com
View Engine • Following are the major View Engines • Razor View Engine • ASPX View Engine • Spark View Engine • NhamlView Engine • Custom View Engine (WebFormViewEngine) http://kb.vteamslabs.com
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
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
_Layout.cshtml File http://kb.vteamslabs.com
Web Controls (Html Helpers) • In Web Forms, we have web controls in System.Web.UII-e Textbox, Dropdownlist, Label, Checkbox, Radiobuttonetc. • 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
Validation Controls (Data Annotations) • In Web forms, we have validation controls to perform validations I-e RequiredFieldValidator, RegularExpressionValidatoretc • 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
Data Annotations http://kb.vteamslabs.com
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
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
Further readings • www.asp.net/mvc • http://www.asp.net/mvc/tutorials/mvc-music-store • http://msdn.microsoft.com/en-us/library/dd381619.aspx • http://msdn.microsoft.com/en-us/library/dd381612.aspx http://kb.vteamslabs.com
Thank you! Questions please? http://kb.vteamslabs.com