• Sayantan Basu


A  ViewModel is a .NET representation of a schema in tridion, means the fields in the schema become properties in the ViewModel. The objective of this blog is to create a view model with all the possible fields of a schema.

So as you can see here. I have created a sample schema with below fields:

Now we will create a viewmodel for that and map properties with all the fields of the schema.

In DD4T 2.0 it is very easy to map your schema with the view model of your web application. If you want to understand what ViewModels are and why they are useful then please check this blog from Trivident team.

Before you start creating the ViewModel class let me give you a small background all the model classes should implement IViewModel interface. So first you have to inherit ViewModelBase class.

For that you have to include –

 using DD4T.ViewModels.ViewModelBase;

Next Map you View Model with the Schema Root Element Name. To achieve that you have to use ContentModel attribute –

[ContentModel(“article”, true)]

Then Inherit the ViewModelBase and Implement the IRenderableViewModel

public class Article : ViewModelBase, IRenderableViewModel          {          }

Also you need to add the RenderData property in your view model for routing purpose.

So your base structure is ready. Now add the properties in the View Model one by one –

Here are the mapping of Schema Fields –TextFieldTextRichTextFieldRich TextResolvedUrlFieldResolved url of linked Component ComponentLinkedComponentFieldValue of linked ComponentDateFieldDateNumberFieldNumberEmbeddedSchemaFieldEmbedded ComponentMultimediaFieldMultimedia Link

Therefore the next step is to add the schema field in our class

[TextField(FieldName = “title”)]        public string Title { get; set; }

Though the FieldName parameter is optional, and is only required if the field name is different from the property name, but I will always suggest to keep it there. It makes our code much easier for others to understand.

Now check how to map a linked component. There might be several scenario when you are mapping a linked component. Let’s discuss one by one

  1. To get the resolved url of the linked component you can use below entry –[ResolvedUrlField(FieldName = “componentLink”)]           public string ComponentLink { get; set; }

  2. But if you want to get the value of the linked component then first you have to create a model class for that linked schema and map it in your model using LinkedComponentTypes parameter –

[LinkedComponentField(FieldName = “componentLink”, LinkedComponentTypes = new [] {typeof(Teaser)})]           public IViewModel ComponentLinkData { get; set; }

Next if you want to provide a flexibility to editor by adding more one type of component as a linked component, than create models for those linked schema and map it like –

[LinkedComponentField(FieldName = “componentLink”, LinkedComponentTypes = new []        {typeof(News),typeof(Teaser)})]public IViewModel ComponentLinkData { get; set; }

In this example you can see it is pointing 2 different model [News, Teaser]. These way you can map n number of possibly linked component.

For Embedded Component it will be like –

[EmbeddedSchemaField(FieldName = “embedded”, EmbeddedModelType = typeof(TestEmbeddedModel))]public TestEmbeddedModel EmbeddedTypeField { get; set; }

So here you can define the type of the Embedded Schema using EmbeddedModelType parameter.

For the rest of schema field it is easy and straight forward. You can  refer the below final class for that –

The final class now looks like this:

namespace MyTestApplication.Models.Component{[ContentModel(“article”, true)]public class Article : ViewModelBase, IRenderableViewModel{[TextField(FieldName = “title”)]public string Title { get; set; } [TextField(FieldName = “meta_title”, IsMetadata = true)]public string MetaTitle { get; set; } [ResolvedUrlField(FieldName = “componentLink”)]public string ComponentLink { get; set; } [LinkedComponentField(FieldName = “componentLink”, LinkedComponentTypes = new [] {typeof(News),typeof(Article)})]public IViewModel ComponentLinkData { get; set; } [RichTextField(FieldName =”body”)]public List<MvcHtmlString> Body { get; set; } [DateField (FieldName = “date”)]public DateTime Date { get; set; } [NumberField (FieldName = “number”)]public double Number { get; set; } [MultimediaField(FieldName = “multimediaLink”)]public Multimedia Image { get; set; } [TextField(FieldName = “checkbox”)]public List<string> Checkbox { get; set; } [TextField(FieldName = “category”)]public List<string> Category { get; set; } [EmbeddedSchemaField(FieldName = “embedded”, EmbeddedModelType = typeof(TestEmbeddedModel))]public TestEmbeddedModel EmbeddedTypeField { get; set; } [RenderData]public IRenderData RenderData { get; set; }}}

#DD4T #DD4T20 #SDLTridion2013SP1 #SDLWeb8

30 views0 comments

Recent Posts

See All

© 2019 by Sayantan Basu