Your SlideShare is downloading. ×
  • Like
03 view fundamentals
Upcoming SlideShare
Loading in...5

Thanks for flagging this SlideShare!

Oops! An error has occurred.


Now you can save presentations on your phone or tablet

Available for both IPhone and Android

Text the download link to your phone

Standard text messaging rates apply

03 view fundamentals



Published in Technology
  • 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 Fundamentals1
  • 2. 2 The Views mission is simple: show theuser the model›  The Controller instantiates the model and sends it to the view. The view simply draws it›  It just has to know how to turn the model into HTML
  • 3. 3 The View is rendered from theControllerpublic ActionResult Whatever()!{! return View();!}!›  Assumes view name is the same as the action›  Looks for it in ›  Views/ControllerName ›  Views/Shared
  • 4. 4 You can specify the name of theview and controller›  SpecifyActionpublic ActionResult Whatever()!{! return View("New");!}!›  Action and Controllerpublic ActionResult Whatever()!{! return View("~/Views/Other/Other.cshtml");!}!
  • 5. Hands-on view rendering
  • 6. Passing data to the view1.  ViewData2.  ViewBag3.  Use strongly-typed views
  • 7. 7 ViewData›  A dictionary<string, object>›  To set in the controllerViewData["Focus"] = new Person() {! FirstName="Leslie", ! LastName="Winkle", ! Occupation="Experimental Physicist" !};!›  To read in the view@{!var person = (Person) ViewData["Focus"];!}!
  • 8. ViewBag is a Dynamic object›  In controller action:ViewBag.Greeting = "Hello World";!return View();!›  In view:<p>@ViewBag.Greeting</p>!
  • 9. Hands-on passing data to the view
  • 10. Strongly-typed views›  In view, add a new top line@model Person›  … or …@model ICollection<Person>›  Then the page recognizes the properties of that class<p>Name: @Model.FirstName @Model.LastName</p><p>@Model.Occupation</p>
  • 11. We pass the model to the viewpublic ActionResult DoSomething(){ var p = new Person() { FirstName = "Amy", MiddleName = "Farrah", LastName = "Fowler", Occupation = "Neurobiologist" }; return View(model);}
  • 12. Hands-on strongly-typed views
  • 13. 13 Conclusion›  The controller generates data to present to the user›  It presents that through the view›  So we must get data from the controller to the view through ›  ViewData ›  ViewBag ›  Strongly-typed Views