03 view fundamentals

  • 188 views
Uploaded on

 

More 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

Views

Total Views
188
On Slideshare
0
From Embeds
0
Number of Embeds
0

Actions

Shares
Downloads
7
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. 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