04 view models

353 views

Published on

Published in: Technology, Business
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
353
On SlideShare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
8
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

04 view models

  1. 1. View models Demystified1
  2. 2. 2 A model is a software representationof a real world object or API that wewant to work with›  a.k.a. – A class!
  3. 3. Remember, the controller Model creates the model andpasses it to the view Controller View Surfer
  4. 4. 4 Passing a model to the view looks likethis:public ActionResult ShowPerson(){ var p = new Person() { FirstName = "Amy", MiddleName = "Farrah", LastName = "Fowler", Occupation = "Neurobiologist" }; return View(p);}
  5. 5. 5 Suppose wehave this page›  What would our model look like?›  Its got task-related things›  Its got task-independent things
  6. 6. 6 We have several options1.  Force our task class to have non-task things2.  Force our non-task classes to also store task-related things3.  Pass two or more models to the view4.  Create a third class that has a task and other non-task related things
  7. 7. 7 A class that is built to work in a view isa ViewModelpublic class UpdateTaskViewModel!{! public int TaskId { get; set; }! public int? TaskCategoryId { get; set; }! public string Notes { get; set; }! public TaskStatus? Status { get; set; }! public DateTime? StartDTime { get; set; }! public DateTime? StopDTime { get; set; }! public Boolean? Billable { get; set; }! public List<SelectListItem> Categories { get; set; }! public List<SelectListItem> Statuses { get; set; }! public int? Duration { get; set; }! public List<Material> Materials { get; set; }!}!
  8. 8. Hands-on view model
  9. 9. 9 Conclusion›  View models are appropriate when you have disparate data that needs to be displayed in the view›  You create a class that holds all the disparate data and use that class as the model for the strongly-typed view

×