1 / 22

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

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.

Download Presentation

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

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Working in Asp.Net MVC 3.0 in comparison to Web Forms • By: ShahidMaqsood http://kb.vteamslabs.com

  2. 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

  3. 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

  4. 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

  5. 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

  6. ActionResult Return Type http://kb.vteamslabs.com

  7. Model • Models hold and manipulate data. • They are usually database entities which are mapped on table fields. http://kb.vteamslabs.com

  8. 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

  9. 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

  10. 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

  11. Add View • View name • View engine • Strongly typed view • Partial view • Layout or master page http://kb.vteamslabs.com

  12. 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

  13. 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

  14. 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

  15. _Layout.cshtml File http://kb.vteamslabs.com

  16. 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

  17. 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

  18. Data Annotations http://kb.vteamslabs.com

  19. 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

  20. 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

  21. 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

  22. Thank you! Questions please? http://kb.vteamslabs.com

More Related