Introduction to MVC 4 03. Adding a View Page

26
Introduction to MVC 4 03. Adding a View Page NTPCUG Tom Perkins, Ph.D.

description

Introduction to MVC 4 03. Adding a View Page. NTPCUG Tom Perkins, Ph.D. This section:. View template files are used to generate HTML responses back to the user We’ll create a view template file using the Razor View engine. Razor files have a . cshtml extension. Razor. - PowerPoint PPT Presentation

Transcript of Introduction to MVC 4 03. Adding a View Page

Page 1: Introduction to MVC 4 03. Adding a  View Page

Introduction to MVC 403. Adding a View Page

NTPCUGTom Perkins, Ph.D.

Page 2: Introduction to MVC 4 03. Adding a  View Page

This section:

• View template files are used to generate HTML responses back to the user

• We’ll create a view template file using the Razor View engine.

• Razor files have a .cshtml extension

View template file

Razor

HTML Response to

user

Page 3: Introduction to MVC 4 03. Adding a  View Page

Change the Index method

• Current Index method returns a string. • Change it to return a View object.

public ActionResult Index() { return View(); }

Controller method or Action Method

Page 4: Introduction to MVC 4 03. Adding a  View Page

Add a View Template

• Right click inside the Index method• Click Add View

Page 5: Introduction to MVC 4 03. Adding a  View Page
Page 6: Introduction to MVC 4 03. Adding a  View Page

Click here

Page 7: Introduction to MVC 4 03. Adding a  View Page

The Add View Dialog BoxLeave

defaults alone

Click

Page 8: Introduction to MVC 4 03. Adding a  View Page

New Folder/File Created …

New Folder

New File

Page 9: Introduction to MVC 4 03. Adding a  View Page

The contents of the Index.cshtml file

Page 10: Introduction to MVC 4 03. Adding a  View Page

Add the following HTML under the <h2> tag.

<p>Hello from our View Template!</p>

The complete MvcMovie\Views\HelloWorld\Index.cshtml file is shown below.

@{ ViewBag.Title = "Index";}

<h2>Index</h2>

<p>Hello from our View Template!</p>

Page 11: Introduction to MVC 4 03. Adding a  View Page

VS 2012 only …

Right click on Index.cshtml

Click on View in Page Inspector

Page 12: Introduction to MVC 4 03. Adding a  View Page

Run the app, browse to:http://localhost:xxxx/HelloWorld

From our View Page

Page 13: Introduction to MVC 4 03. Adding a  View Page

Change the Layout page

(master)

Shared Folder all pages use

Click on _Layout.cshtml

Find the @Render

Body() line

Page 14: Introduction to MVC 4 03. Adding a  View Page

RenderBody

• Placeholder• All view-specific pages show up here• View pages are “wrapped” in layout page• When you select the About link– The Views\Home\About.cshtml view is rendered

inside the RenderBody method.

Page 15: Introduction to MVC 4 03. Adding a  View Page

Change the site title

• Change “your logo here” to “MVC Movie”:

<div class="float-left"> <p class="site-title">@Html.ActionLink("MVC Movie", "Index", "Home")</p></div>

Replace the title element:

<title>@ViewBag.Title - Movie App</title>

Page 16: Introduction to MVC 4 03. Adding a  View Page

Run the app; also check the “About” page

Note the changed title

Changes in the Layout template to the title will be reflected in all

the web pages

Page 17: Introduction to MVC 4 03. Adding a  View Page

Change the title of the Index View

@{ ViewBag.Title = "Movie List";}

<h2>My Movie List</h2>

<p>Hello from our View Template!</p>

Open MvcMovie\Views\HelloWorld\Index.cshtmlChange Page title

Change Primary Heading

Change Secondary Heading

ViewBag is an object in

the view template

Page 18: Introduction to MVC 4 03. Adding a  View Page

The changed page …

Changed Page title

Changed Primary Heading

Changed Secondary Heading

Page 19: Introduction to MVC 4 03. Adding a  View Page

Passing data from the Controller to the View

• Controller classes– Invoked for an incoming URL request– Where you write code to:• Handle incoming browser requests• Retrieve data from a database• Decide what type of response to send back to the

browser– Call a View class to generate and format an HTML

response back to the browser

Page 20: Introduction to MVC 4 03. Adding a  View Page

Best Practices

• Controllers provide data to views.• A view template should never perform

business logic or interact with a database directly.

• View should work only with data provided by controller (Separation of Concerns)

Controller

View

Data

Page 21: Introduction to MVC 4 03. Adding a  View Page

Pass data from a Controller to a View via the ViewBag …

HelloWorldController, Welcome Action

URL containing Message,

NumTimes ViewBag Object

Parameters: Message, NumTimes

Welcome ViewHTML Browser

Page 22: Introduction to MVC 4 03. Adding a  View Page

Modify the Welcome method in the HelloWorldController

using System.Web;using System.Web.Mvc;

namespace MvcMovie.Controllers{ public class HelloWorldController : Controller { public ActionResult Index() { return View(); }

public ActionResult Welcome(string name, int numTimes = 1) { ViewBag.Message = "Hello " + name; ViewBag.NumTimes = numTimes;

return View(); } }}

Automatic Binding to

query string

ViewBag can contain

anything (dynamic)

Page 23: Introduction to MVC 4 03. Adding a  View Page

Create a Welcome view template

• F6- compile the project• Inside the Welcome method:– Right-Click– Select Add View– Click Add on the Add View dialog box– The Welcome.cshtml will be added to the project

Page 24: Introduction to MVC 4 03. Adding a  View Page

Add logic to display data in a loop

• Modify Welcome.cshtml …@{ ViewBag.Title = "Welcome";}

<h2>Welcome</h2>

<ul> @for (int i=0; i < ViewBag.NumTimes; i++) { <li>@ViewBag.Message</li> } </ul>

• Run the app:– http://localhost:xx/HelloWorld/Welcome?name=Scott&numtimes=4

Page 25: Introduction to MVC 4 03. Adding a  View Page
Page 26: Introduction to MVC 4 03. Adding a  View Page

We’ve built a Controller and a View …We’ll build a Model next …