Razor is Sharp C? NuGet, it? Notes from MvcConf 2 and Pluralsights Videos

Transitioning to MVC3 has been a great experience so far.  Changing from <%= to @ has been a dream come true.  It might not seem like it since its only a couple characters, but seriously, it works much better, and I can already see the cut down in coding time. 

My brain hurts from watching the MvcConf live webcast sessions most of the day yesterday, and Pluralsight MVC3 videos today (free until 9PM tonight).  Scott Guthrie did a great job on the Keynote at MvcConf and all the presentations I watched were well done.  I feel like I went to school the last couple days, and actually it felt pretty good.  Below are my notes from the PluralSight videos, highlighting things I wanted to remember about Razor and MVC3:

  • Razor - A general purpose templating engine
  • Data Annotations - Attributes that can be applied to properties of models can be used for Data Validation (both client / server-side)  eg. [Required] or [Range(1,4)]
  • ModelState.IsValid - Checks model validation
  • ClientValidationEnabled - flag in appSettings that determines if validation is handled on client-side (via jQuery) or server-side
  • UnobstrusiveJavaScriptEnabled - flag for ?
  • Implicit to Explicit Code Nuggets - You can use @(variable) to ensure the code is explcit and kept seperate from any following text
  • Transitioning from Razor to Text - Using tags ... You can use the tags to help the Razor parser when it gets tripped up in coding blocks
  • @Html.Raw - Do not html encode the code.
  • @Html.Partial - Returns string instead of writing directly to Response (Reminder: Partial views vs jQuery templates - usages, SEO, etc)
  • @RenderSection - Sections that can be defined within each view.  Using required:false lets you make defining sections optional
  • IsSectionDefined - Lets you check if a section is defined within a view.  This is useful when you want to add default content when a section isn't defined within a view.
  • _ViewStart.cshtml - Lets you define code before every View renders
  • system.web.webPages.razor - Dedicated section for Razor in the Web.Config file found within the Views folder

Action Filters

Can be placed at the ActionResult or Or can be placed at the controller level

  • [HandleError] - Allows pre/post processing to an action Friendly error view instead of yellow-screen of death
  • [Authorize] - Requires authorization to proceed _

Global Action Filters
_

  • New in MVC3 - Ability to apply Global Action Filters
  • Register Global filters during application startup
    • eg. GlobalFilters.Filters.Add(new HandleErrorAttribute());

Caching

  • New in MVC3?  Caching can now be applied to child actions.  Useful for caching on partial views.
  • [OutputCache(Duration=10)] - Set time for caching
  • [ChildActionOnly] - Ensure someone doesn't make a direct request to this action

ViewBag vs ViewData

  • ViewData is  dictionary of name/value pairs
  • ViewBag is a dynamic

New Action Results in MVC3

  • HttpNotFoundResult (404)
  • HttpRedirectResult (301 permenanent redirect)
  • HttpStatusCodeResult

RequestValidation

  • [ValidateInput(false)] - Turns off all verification to ActionResult.  All or nothing approach, which can be problematic and hazardous.
  • [AllowHtml] - New in MVC3 a granular verification for view model which can be attributed to properties

Models Overview

  • Data Annotations - Required, StringLength, RegularExpression, Range
  • Client Validation - Improvements in MVC3
  • Remote Validation - New in MVC3 invokes server-side code, eg. validation that requires database information
  • Self Validation - View model object itself

Data Annotations - New

  • [Compare("varToCompare")] - New in .NET 4, allows to compare two properties
  • A second new one is ... ?

Client Validation

  • @Html.EnableClientValidation
  • @Html.EnableUnobstrusiveJavaScript

Remote Validation

  • [Remote("ControllerAction", Named Parms)]