Your SlideShare is downloading. ×
0
View  Fundamentals1
2	The Views mission is simple: show theuser the model›  The   Controller instantiates the model and sends it to    the vi...
3	The View is rendered from theControllerpublic ActionResult Whatever()!{!     return View();!}!›  Assumes view name is t...
4	You can specify the name of theview and controller›  SpecifyActionpublic ActionResult Whatever()!{!    return View("New...
Hands-on view rendering
Passing data to the view1.    ViewData2.    ViewBag3.    Use strongly-typed views
7	ViewData›  A dictionary<string, object>›  To set in the controllerViewData["Focus"] = new Person() {!     FirstName="L...
ViewBag is a Dynamic object›  In controller action:ViewBag.Greeting = "Hello World";!return View();!›  In view:<p>@ViewB...
Hands-on passing data to the view
Strongly-typed views›  In view, add   a new top line@model Person›  …   or …@model ICollection<Person>›  Then the page ...
We pass the model to the viewpublic ActionResult DoSomething(){  var p = new Person() {     FirstName = "Amy",     MiddleN...
Hands-on strongly-typed views
13	Conclusion›  The  controller generates data to present to the user›  It presents that through the view›  So we must ...
Upcoming SlideShare
Loading in...5
×

03 view fundamentals

212

Published on

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

  • Be the first to like this

No Downloads
Views
Total Views
212
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
8
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Transcript of "03 view fundamentals"

  1. 1. View Fundamentals1
  2. 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. 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. 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. 5. Hands-on view rendering
  6. 6. Passing data to the view1.  ViewData2.  ViewBag3.  Use strongly-typed views
  7. 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. 8. ViewBag is a Dynamic object›  In controller action:ViewBag.Greeting = "Hello World";!return View();!›  In view:<p>@ViewBag.Greeting</p>!
  9. 9. Hands-on passing data to the view
  10. 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. 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. 12. Hands-on strongly-typed views
  13. 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
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×