p2p.wrox.com Forums

p2p.wrox.com Forums (http://p2p.wrox.com/index.php)
-   BOOK: Beginning ASP.NET Web Pages with WebMatrix (http://p2p.wrox.com/forumdisplay.php?f=662)
-   -   Chapter 3 - Passing Data from the Layout to a Section (http://p2p.wrox.com/showthread.php?t=89458)

hanci February 28th, 2013 02:54 PM

Chapter 3 - Passing Data from the Layout to a Section
 
The code for the Footer as show on the top of page 73 is:

Code:

@RenderPage("~/Shared/_Footer.cshtml", new (Year = DateTime.Now.Year))
This gave me two errors:

(1) "Compiler Error Message: CS1031: Type expected"

And then, after I added a type:

(2) "Compiler Error Message: CS0103: The name 'Year' does not exist in the current context"

The code that worked finally:

Code:

@RenderPage("~/Shared/_Footer.cshtml", new DateTime (Page.Year = DateTime.Now.Year))
I couldn't find the errata, is this an error in the code in the book (which, I think, is very good, in every aspect!)?

Imar February 28th, 2013 03:06 PM

Hi there,

I don't have the book right here with me so I can't check. However, the downloadable code has this:

@RenderPage("~/Shared/_Footer.cshtml", new {Year = DateTime.Now.Year})

Notice it uses curly braces {} around the anonymous type for the date instead if parentheses () as per your example. Maybe it's just a typo?

Imar

hanci February 28th, 2013 05:41 PM

Yes, you're right, it was the curly brackets (my reading error) that caused the problem. But the solution I found for getting it to work anyway helped me to find a way to use the alternative notation for page data, which gives errors with the curly bracket notation and which I prefer because it stands out clearer in the code:

Code:

@RenderPage("~/Shared/_Footer.cshtml", new DateTime (PageData["Year"] = DateTime.Now.Year))
and in the footer:

Code:

© 2012-@PageData["Year"], SomeName
This works fine, but maybe it is a clumsy way to get this result?

Hans

Imar March 2nd, 2013 07:38 AM

Yes, it's a bit weird. You're creating a new DateTime that you never use, only to assign a year to PageData.

And yes, constructor syntac (e.g. new DateTime(...) uses parentheses instead of the curly braces.

Cheers.

Imar

NickF May 12th, 2017 08:21 AM

Imar
FYI, the book is correct, I made the same typo as I didn't spot the curly braces either.
Nick


All times are GMT -4. The time now is 10:25 PM.

Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.