03 view fundamentals
Upcoming SlideShare
Loading in...5
×
 

Like this? Share it with your network

Share

03 view fundamentals

on

  • 397 views

 

Statistics

Views

Total Views
397
Views on SlideShare
397
Embed Views
0

Actions

Likes
0
Downloads
7
Comments
0

0 Embeds 0

No embeds

Accessibility

Categories

Upload Details

Uploaded via as Adobe PDF

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

03 view fundamentals Presentation 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