Your SlideShare is downloading. ×
0
Models
Models
Models
Models
Models
Models
Models
Models
Models
Models
Models
Models
Models
Models
Models
Models
Models
Models
Models
Models
Models
Models
Models
Models
Models
Models
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply

Models

2,441

Published on

ASP.NET MVC .30 - Models

ASP.NET MVC .30 - Models

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
2,441
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
101
Comments
0
Likes
0
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

  • 1. Models in MVC Eyal Vardi CEO E4D Solutions LTD Microsoft MVP Visual C# blog: www.eVardi.com
  • 2. Models in MVC Eyal Vardi CEO E4D Solutions LTD Microsoft MVP Visual C# blog: www.eVardi.com
  • 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. 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. 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. 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. 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. 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. 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. Model Templates© 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: Eyal@E4D.co.il
  • 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. 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. 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. 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. 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. 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. 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. Bind Attribute© 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: Eyal@E4D.co.il
  • 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. 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. 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. Manually Invoking Model Binding© 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: Eyal@E4D.co.il
  • 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. 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. Custom Binder© 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: Eyal@E4D.co.il
  • 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

×