Models

2,701 views

Published on

ASP.NET MVC .30 - Models

0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
2,701
On SlideShare
0
From Embeds
0
Number of Embeds
4
Actions
Shares
0
Downloads
106
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

Models

  1. 1. Models in MVC Eyal Vardi CEO E4D Solutions LTD Microsoft MVP Visual C# blog: www.eVardi.com
  2. 2. Models in MVC Eyal Vardi CEO E4D Solutions LTD Microsoft MVP Visual C# blog: www.eVardi.com
  3. 3. Agenda  Integrating the Model & Controller  Model Binders  Model Validation© 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: Eyal@E4D.co.il
  4. 4. What Is a Model? Model Template Model Deserialization HTML View Controller HTTP Template© 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: Eyal@E4D.co.il
  5. 5. Model Attributes  Scaffolding  [ScaffoldColumn]  View  [HiddenInput]  [Display]  [DisplayName], Attribute on a Class  [DataType(...)]  [UIHint(...)]  [AdditionalMetadata( key , value )]© 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: Eyal@E4D.co.il
  6. 6. Display Attribute [DisplayName("Book")] Model public class E4DBook { [Display(Name = "Title")] public string E4DTitle { get; set; } } <h2>@Html.LabelForModel()</h2> View @Html.EditorForModel() Or @Html.LabelFor(m => m.E4DTitle)© 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: Eyal@E4D.co.il
  7. 7. DataType Attribute  DateTime  Password  Date  Url  Time  EmailAddress  Text  MultilineText© 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: Eyal@E4D.co.il
  8. 8. UIHint Attribute  Use the UIHint attribute to specify the template we want to use to render HTML for a property. [DisplayName("Book")] Model public class E4DBook { [Display(Name = "Title")] [DateType(DataType.MultilineText)] [UIHint("MultilineText")] public string E4DTitle { get; set; } }© 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: Eyal@E4D.co.il
  9. 9. Built in View Template  Boolean  Object  Collection  Password  Decimal  String  EmailAddress  Text  HiddenInput  Url  Html  MultilineText© 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: Eyal@E4D.co.il
  10. 10. Model Templates© 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: Eyal@E4D.co.il
  11. 11. What Is a Model? Model Binding Model Deserialization HTML View Controller HTTP Template© 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: Eyal@E4D.co.il
  12. 12. HTTP to .NET Classes public ActionResult Index(Reservation reservation) { // TODO BL. return View( reservation ); } Deserialization© 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: Eyal@E4D.co.il
  13. 13. Model Binders  Binders are like type converters, because they can convert HTTP requests into objects that are passed to an action method.© 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: Eyal@E4D.co.il
  14. 14. DefaultModelBinder Class  Translate HTTP request into complex models, including nested types and arrays.  URL  Form data, Culture - Sensitive 1. Request.Form 2. RoutedData.Values (Key,Value) 3. Request.QueryString 4. Request.Files© 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: Eyal@E4D.co.il
  15. 15. Binding to Complex Types [DisplayName("Book")] Model public class E4DBook { [Display(Name = "Title")] public string E4DTitle { get; set; } public Reservation MyReservation { get; set; } } @Html.LabelFor(model => model.MyReservation.FlightNum) View @Html.EditorFor(model => model.MyReservation.FlightNum) Html <label for="MyReservation_FlightNum">FlightNum</label> <input type="text" value="" id = "MyReservation_FlightNum" name = "MyReservation.FlightNum" />© 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: Eyal@E4D.co.il
  16. 16. Specifying Custom Prefixes Controller public ActionResult Index( Reservation res1 , [Bind(Prefix="myRes")] Reservation res2 ) { ... } <!– First Arg --> Html <label for="FlightNum">FlightNum</label> <input type="text" id="FlightNum" name="FlightNum" /> ... <!– Second Arg --> <label for="MyRes_FlightNum">FlightNum</label> <input type="text" id="MyRes_FlightNum" name="MyRes.FlightNum" />© 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: Eyal@E4D.co.il
  17. 17. Bind Attribute (“View”)  Represents an attribute that is used to provide details about how model binding to a parameter should occur. [Bind(Include = "FlightNum, TravelDate)] public class Reservation { public int FlightNum { get; set; } public DateTime TravelDate { get; set; } public int TicketCount { get; set; } public int DiscountPercent { get; set; } }© 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: Eyal@E4D.co.il
  18. 18. Bind Attribute© 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: Eyal@E4D.co.il
  19. 19. Binding to a String Collections Controller public ActionResult Index( List< string > reservation ) { ... } Html <input type="text" name="reservation" value="…" /> <input type="text" name="reservation" value="…" /> <input type="text" name="reservation" value="…" /> <input type="text" name="reservation" value="…" />© 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: Eyal@E4D.co.il
  20. 20. Binding to a Collections Controller public ActionResult Index( List<Reservation> reservation ) { ... } <!– Option I--> Html <input type="text" name="[0].Name" /> <input type="text" name="[1].Name" /> <!– Option II --> <input type="hidden" name="Index" value="f1" /> <input type="text" name="[f1].Name" /> <input type="hidden" name="Index" value="f2" /> <input type="text" name="[f2].Name" />© 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: Eyal@E4D.co.il
  21. 21. Binding to a Dictionary Controller public ActionResult Index( Dictionary<Reservation> reservation ) { ... } Html <input type="hidden" name="[0].key" value="f1" /> <input type="text" name="[0].value.Name" /> <input type="hidden" name="[1].key" value="f2" /> <input type="text" name="[1].value.Name" />© 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: Eyal@E4D.co.il
  22. 22. Manually Invoking Model Binding© 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: Eyal@E4D.co.il
  23. 23. Custom Model Binders  ASP.NET MVC allows us to override both the default model binder, as well as add custom model binders. ModelBinders.Binders.DefaultBinder = new CustomDefaultBinder(); ModelBinders.Binders.Add( typeof(DateTime), new DateTimeModelBinder() );© 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: Eyal@E4D.co.il
  24. 24. ModelBinder Attribute  Represents an attribute that is used to associate a model type to a model-builder type. public ActionResult Contact( [ModelBinder(typeof(ContactBinder))] Contact contact ) { ViewData["name"] = contact.Name; ViewData["email"] = contact.Email; ViewData["message"] = contact.Message; ViewData["title"] = "Succes!"; return View(); }© 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: Eyal@E4D.co.il
  25. 25. Custom Binder© 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: Eyal@E4D.co.il
  26. 26. Integrating The Model & Controller Deserialization public ActionResult Index(Reservation reservation) { int totalCost = reservation.TicketCount * 100; if (reservation.DiscountPercent != 0) { BL totalCost -= (totalCost * reservation.DiscountPercent / 100); } ViewBag.TotalCost = totalCost; Index.cshtml @model Reservation return View( reservation ); } Model© 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: Eyal@E4D.co.il

×