Download presentation
Presentation is loading. Please wait.
Published byPhạm Quốc Modified over 5 years ago
1
05 | Customizing Views Jon Galloway | Development Platform Evangelist
Christopher Harrison | Content Development Manager
2
Changing the Outlook on Views
How Views are Found Views and Models Razor Syntax HtmlHelper Layouts
3
How Views are Found
4
Finding Views “It just works” – Jon Galloway
Views reside in the Views folder Subfolders Name of the controller Shared
5
View Resolution in Action
public class AlbumController : Controller { public ActionResult Index() { return View(); } } Look for a view named Index in a folder named Album Look for a view named Index in a folder named Shared Error
6
View Resolution in Action Again
public class AlbumController : Controller { public ActionResult Index() { return View("Default"); } } Look for a view named Default in a folder named Album Look for a view named Default in a folder named Shared Error
7
View Resolution
8
Views and Models
9
Views and Models Views have a Model property
Type is set declaration Note the CaSiNg Advanced note Views do not need to have a typed model Useful for creating dynamic views
10
Razor Syntax
11
Razor Syntax @ indicates server-side code
MVC runtime determines meaning based on context <a generates appropriate HTML Use
12
Razor Syntax
13
HtmlHelper
14
HtmlHelper Helps generate HTML Uses attributes on model Display names
Formatting Input elements
15
HtmlHelper and Lambdas
@Html.DisplayFor(model => model.Name) MVC uses reflection Reflection isn’t evil! Helper methods need the property, not the value @Html.DisplayFor(Model.Name) would pass the value of Name => m.Name) for easier reading
16
Displaying Data DisplayNameFor DisplayFor DisplayName attribute
Display attribute, Name property DisplayFor Uses DisplayFormat (if applicable)
17
Displaying Data
18
Creating Forms HtmlHelper.BeginForm() Why not just use a form element?
URLs can always change Parameters Action name Controller name Form method Get Post
19
Accepting Input LabelFor InputFor Creates a label element
Useful for touch InputFor Creates input element Uses HTML5 based on DataType attribute
20
Basic Forms
21
Validation ValidationMessageFor ValidationSummary
Display error message next to text box ValidationSummary Display all error messages in one location
22
Adding Validation
23
Layouts
24
Welcome! My Music Store New This Week! Popular Items FAQs
Deleted Smiths Singles Original - not rereleased - Frank Zappa Albums Welcome! Feel free to look around. All records are available for sale. Popular Items The Three EPs The Beta Band I Just Called to Say I Love You Stevie Wonder FAQs Q: Do you have soul? A: That all depends.
25
Organization and Consistency
Use layouts to ensure consistent page structure Layout methods RenderBody() Renders anything in a view not in a section RenderSection(name, required) Allow views to add specific sections Scripts Banners Sidebars name to create section in view Note the casing
26
Using Layouts
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.