Join Jess Chadwick for an in-depth discussion in this video Adding code blocks to a Razor page, part of ASP.NET Core: Razor Pages.
- [Instructor] In the previous video,…I showed you how to use the @ syntax to evaluate…in-line code snippets and render the result…as content in the page.…However, there will be plenty of times…where you'll want to execute some logic…that doesn't result in rendering content to the page,…for example, creating a variable to store this list…of recipes that I've retrieved right there in the middle…of that foreach loop.…Luckily, Razor offers a syntax called a code block…which allows you to do exactly that and it looks like this.…
It's just the @ symbol followed by open and closed brackets,…in between which you can write whatever code you like,…and it's a perfect place to define things like variables.…So, to introduce a variable to store the list of recipes…and clean up this foreach loop, I can simply define…a new variable in the code block.…Then I can move the call to the GetAll method…to populate that new variable,…and then finally reference that new variable…in my foreach loop.…
The nice thing about code blocks is that you're free…
- Creating a new application
- Setting up pages
- Rendering dynamic content
- Reusing markup with layouts
- Increasing the maintainability of pages
- Processing data
- Validating input
- Securing an application
Skill Level Intermediate
Deploying ASP.NET Core Applicationswith Nate Barbettini57m 57s Intermediate
Building Web APIs with ASP.NET Corewith Chris Woodruff1h 7m Intermediate
ASP.NET Core: Test-Driven Developmentwith Richard Rout37m 30s Intermediate
1. Razor Pages Intro
What is Razor Pages?3m 5s
2. Razor Pages Basics
3. Reusing Markup with Layouts
4. Building Maintainable Applications
5. Working with Data
6. Validating User Inputs and Securing Your Application from Unauthorized Users
- Mark as unwatched
- Mark all as unwatched
Are you sure you want to mark all the videos in this course as unwatched?
This will not affect your course history, your reports, or your certificates of completion for this course.Cancel
Take notes with your new membership!
Type in the entry box, then click Enter to save your note.
1:30Press on any video thumbnail to jump immediately to the timecode shown.
Notes are saved with you account but can also be exported as plain text, MS Word, PDF, Google Doc, or Evernote.