-
Notifications
You must be signed in to change notification settings - Fork 3
2. Introduction
pdebacker edited this page Jun 6, 2013
·
2 revisions
When you build web sites using Umbraco and don’t want to use XLST to generate the HTML output, there are a few other options. When you choose to use Razor or Mustache as view engine, you will find yourself constructing view models and writing a lot of plumbing code. Most of the time this means copying a Node property value into a model class property. Code like:
var model = new MyViewModel();
INode currentNode = Node.GetCurrent();
IProperty property = null;
property = currentNode.GetProperty("pageTitle");
if (property != null)
model.PageTitle = property.Value;
With Umbraco Code Generation this plumbing code is generated automatically using T4 templates. The Models (POCO’s) and a ModelFactory class are automatically generated. The ModelFactory class can fill any custom model, as long as the properties are decorated with the appropriate attributes. Filling a view model is then reduced to the following two lines of code:
INode currentNode = Node.GetCurrent();
var model = ModelFactory.CreateModel<MyViewModel>(currentNode);