• Share
  • Email
  • Embed
  • Like
  • Save
  • Private Content
03 view fundamentals
 

03 view fundamentals

on

  • 342 views

 

Statistics

Views

Total Views
342
Views on SlideShare
342
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 03 view fundamentals Presentation Transcript

    • View Fundamentals1
    • 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 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 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");!}!
    • 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="Leslie", ! LastName="Winkle", ! Occupation="Experimental Physicist" !};!›  To read in the view@{!var person = (Person) ViewData["Focus"];!}!
    • ViewBag is a Dynamic object›  In controller action:ViewBag.Greeting = "Hello World";!return View();!›  In view:<p>@ViewBag.Greeting</p>!
    • 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 recognizes the properties of that class<p>Name: @Model.FirstName @Model.LastName</p><p>@Model.Occupation</p>
    • We pass the model to the viewpublic ActionResult DoSomething(){ var p = new Person() { FirstName = "Amy", MiddleName = "Farrah", LastName = "Fowler", Occupation = "Neurobiologist" }; return View(model);}
    • 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 get data from the controller to the view through ›  ViewData ›  ViewBag ›  Strongly-typed Views