• Like
04 view models
Upcoming SlideShare
Loading in...5

04 view models

Uploaded on


More in: Technology , Business
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Be the first to comment
    Be the first to like this
No Downloads


Total Views
On Slideshare
From Embeds
Number of Embeds



Embeds 0

No embeds

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

    No notes for slide


  • 1. View models Demystified1
  • 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. Remember, the controller Model creates the model andpasses it to the view Controller View Surfer
  • 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 Suppose wehave this page›  What would our model look like?›  Its got task-related things›  Its got task-independent things
  • 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 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. Hands-on view model
  • 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