34
Razor in MVC (View) Eyal Vardi CEO E4D Solutions LTD Microsoft MVP Visual C# blog: www.eVardi.com

Views

Embed Size (px)

DESCRIPTION

ASP.NET MVC 32.0

Citation preview

Page 1: Views

Razor in MVC (View)

Eyal VardiCEO E4D Solutions LTDMicrosoft MVP Visual C#blog: www.eVardi.com

Page 2: Views

© 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: [email protected]

Agenda

Razor Syntax

Master-Page View

Partial View

Helper Classes

Page 3: Views

© 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: [email protected]

Razor Syntax

Page 4: Views

© 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: [email protected]

Razor Design Goals

Compact, Expressive, and Fluid

Easy to Learn

Is not a new language

Works with any Text Editor

Has great Intellisense

Unit Testable

Page 5: Views

© 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: [email protected]

What To Choose?

Page 6: Views

© 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: [email protected]

ASPX vs. Razor

Page 7: Views

© 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: [email protected]

ASPX vs. Razor

Page 8: Views

© 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: [email protected]

Single Statement Blocks

<!-- Single statement blocks -->@{ var total = 7; }@{ var myMessage = "Hello World"; } <!-- Inline expressions --><p>The value of your account is: @total </p><p>The value of myMessage is: @myMessage</p>

Page 9: Views

© 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: [email protected]

Multi-Statement Block

<!-- Multi-statement block -->@{ var greeting = "Welcome to our site!"; var weekDay = DateTime.Now.DayOfWeek; var greetingMessage = greeting + " Today is: " + weekDay;}

<p>The greeting is: @greetingMessage</p>

Page 10: Views

© 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: [email protected]

Lines of Content

@for( int i=0 ; i < 3 ; i++ ){ @:Line <b>@i</b> of content <br/>}

// Option II

@for( int i=0 ; i < 3 ; i++ ){ <text> Line <b>@i</b> of content <br/> </text> }

Page 11: Views

© 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: [email protected]

Code Comments

@{ @* This is a comment. *@ @* var theVar = 17; *@ }

@* This is a multiline code comment. It can continue for any number of lines. *@

@* A one-line code comment. *@

@{ // This is a comment. var myVar = 17; /* This is a multi-line comment that uses C# commenting syntax. */ }

Page 12: Views

© 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: [email protected]

Master Page

Page 13: Views

© 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: [email protected]

RenderPage

Page 14: Views

© 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: [email protected]

 @Html.Partial()

<html> <body> @Html.Partial("PartialIndex");

@RenderBody() 

</body></html>

E4D.cshtml

Page 15: Views

© 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: [email protected]

Master Page

<!DOCTYPE html><html> <body> <div id="Header">My Header</div> <div id="main"> @RenderBody() </div> </body></html>

E4D.cshtml

@{ Layout = "~/Views/Shared/E4D.cshtml"; ViewBag.Title = "Home Page";}<h2>@ViewBag.Message</h2><p> @{ for( int i=0 ; i < 5 ; i++ ) { <b>Hello World.</b><br/> } }</p>

Index.cshtml

Page 16: Views

© 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: [email protected]

Master Page

Page 17: Views

© 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: [email protected]

The _ViewStart File

Can be used to define common view code that you want to execute at the start of each View’s rendering.

@{ Layout = "~/Views/Shared/E4D.cshtml";}

_ViewStart.cshtml

Page 18: Views

© 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: [email protected]

Layout Section

<html>      <head>ASP.NET MVC 3.0</head>      <body>          @RenderSection("Header")          @RenderBody()                 @RenderSection("Footer", required: false)      </body> </html>

E4D.cshtml

@{ Layout = "~/Views/Shared/_Layout.cshtml"; } <html>      <head>ASP.NET MVC 3.0</head>      <body>          @section Header {  <div>Header content</div> } <div>Body Content</div>          @section Footer {  <div>Footer content</div> }      </body> </html>

Index.cshtml

Page 19: Views

© 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: [email protected]

Master Page & Sections

Page 20: Views

© 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: [email protected]

IsSectionDefined method

The IsSectionDefined method returns true if a child content page defined a section.

<html> <body> @RenderBody()  @if ( IsSectionDefined("OptionalContent") )  {     @RenderSection("OptionalContent") } else  {     <div>Default content</div> } </body></html>

E4D.cshtml

Page 21: Views

© 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: [email protected]

Use Razor Inline Templates

public static class SectionExtensions     {          private static readonly object _o = new object();          public static HelperResult RenderSection( this WebPageBase page, string sectionName,   Func<object, HelperResult> defaultContent)    {              if (page.IsSectionDefined(sectionName))                      return page.RenderSection(sectionName);                     else              return defaultContent(_o);                       }     }

<body>          ... @this.RenderSection( "Footer", @<div>Default content</div>)     </body>

Page 22: Views

© 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: [email protected]

Passing Data to Layout Pages

Page 23: Views

© 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: [email protected]

PageData Collection<html>      <head>@PageData["Title"]</head>      <body>          @RenderSection("Header")          @RenderBody()                 @RenderSection("Footer", required: false)      </body> </html>

E4D.cshtml

@{ Layout = "~/Views/Shared/_Layout.cshtml";  @PageData["Title"] = “Passing Datat to Layout”; } <html>      <head>ASP.NET MVC 3.0</head>      <body>          @Section("Header"){  <div>Header content</div> } <div>Body Content</div>          @Section("Footer"){  <div>Footer content</div> }      </body> </html>

Index.cshtml

Page 24: Views

© 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: [email protected]

HTML Helper

Page 25: Views

© 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: [email protected]

HtmlHelper Class

Helper methods and extensions are called using the Html property of the view, which is an instance of the HtmlHelper class. Extension methods for the HtmlHelper class are in the

System.Web.Mvc.Html namespace.

@Html.CheckBox("Test it");

Page 26: Views

© 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: [email protected]

Basic HTML Helpers

ActionLink

BeginForm

CheckBox

DropDownList

Hidden

ListBox

Password

RadioButton

TextArea

TextBox

Page 27: Views

© 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: [email protected]

Basic HTML Helpers

Page 28: Views

© 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: [email protected]

Advance HTML Helpers

Chart Renders a chart, offering the same features as the chart control in

ASP.NET 4.

WebGrid Renders a data grid, complete with paging and sorting functionality.

Crypto Uses hashing algorithms to create properly salted and hashed

passwords.

WebImage

WebMail

Page 29: Views

© 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: [email protected]

Advance HTML Helpers

Page 30: Views

© 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: [email protected]

@helper Syntax

Page 31: Views

© 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: [email protected]

@helpers Across Multiple Views

Page 32: Views

© 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: [email protected]

Custom HTML Helper

public static class LabelExtensions{   public static string Label(            this HtmlHelper helper, string target, string text)   {       return String                .Format("<label for='{0}'>{1}</label>", target, text);   }}

Page 33: Views

© 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: [email protected]

TagBuilder Class

Represents a class that is used by HTML helpers to build HTML elements.

public static class ImageHelper{   public static MvcHtmlString Image(this HtmlHelper helper, string id,  string url, string alt)   {       var builder = new TagBuilder("img");       builder.GenerateId(id);

       // Add attributes       builder.MergeAttribute("src", url);       builder.MergeAttribute("alt", alt);                  return new MvcHtmlString( builder.ToString(TagRenderMode.SelfClosing) );   }}

Page 34: Views

© 2010 E4D LTD. All rights reserved. Tel: 054-5-767-300, Email: [email protected]

Custom HTML Helper