Table of Contents

Class ItemViewModel

Namespace
Dynamicweb.Frontend
Assembly
Dynamicweb.dll

The ItemViewModel class provides a robust way to represent the rendering context for items within Razor templates in Dynamicweb. This class serves as the bridge between your data and the Razor templates, offering easy access to the fields, values, and other metadata associated with the item being rendered.

public class ItemViewModel : ViewModelBase
Inheritance
ItemViewModel
Inherited Members

Remarks

With this view model, developers can efficiently interact with the item’s data, retrieving field values in various types (such as strings, booleans, dates, and more), and render them dynamically within the template.

The ItemViewModel offers a wide array of methods for accessing these fields, including type-specific getters (e.g., GetString(string), GetBoolean(string), GetDateTime(string)), as well as helper methods like TryGetField(string, out ItemFieldViewModel) and TryGetImageFiles(string, out IEnumerable<ImageFileViewModel>), which facilitate safe and flexible data retrieval.

This structure ensures that your Razor templates can be both powerful and clean, with minimal logic needed to handle data retrieval, allowing you to focus on presenting the content.

By leveraging the ItemViewModel, you can create highly dynamic and reusable templates that respond to the specific data associated with each item, enhancing both the flexibility and maintainability of your web applications.

Constructors

ItemViewModel()

Initializes a new instance of the ItemViewModel class.

public ItemViewModel()

Properties

Fields

Gets a list of fields and their values for this item.

public IList<ItemFieldViewModel> Fields { get; set; }

Property Value

IList<ItemFieldViewModel>

If you have defined an item type with 3 fields, you'll get a list of three fields and their values of this item instance

Id

Gets the item id.

public string Id { get; set; }

Property Value

string

E.g. '128'

Gets a link (URL) to the page or paragraph that this item is attached to. If the item is not a page or paragraph this property is empty

public string Link { get; set; }

Property Value

string

The link. e.g. '/home' (Default.aspx?ID=1 if not using friendly URLs) or '/home#345'

PageID

Gets the page id if the item is attached to a page, row or a paragraph. If the item is attached to an area or user, this field is 0

public int PageID { get; set; }

Property Value

int

E.g. '150'

ParagraphID

Gets the paragraph id if the item is attached to a paragraph. If the item is attached to an area, page, row or user, this field is 0

public int ParagraphID { get; set; }

Property Value

int

E.g. '1028'

SystemName

Gets the system name of the item type.

public string SystemName { get; set; }

Property Value

string

E.g. 'Swift_Page'

Methods

GetBoolean(string)

Gets the value of the specified field as a boolean.

public bool GetBoolean(string systemName)

Parameters

systemName string

The field system name.

Returns

bool

true or false, which reflects the value stored in specified field. Fallback is false

Remarks

This method uses the GetField(string) method to retrieve the field and then attempts to get its value converted to the boolean type. If the field does not exist, false is returned.

GetButton(string)

Gets a button view model if the field is a button field

public ButtonViewModel GetButton(string systemName)

Parameters

systemName string

The field system name.

Returns

ButtonViewModel

See ButtonViewModel

GetColor(string)

Returns a color view model of the field.

public ColorViewModel GetColor(string systemName)

Parameters

systemName string

The field system name.

Returns

ColorViewModel

See ColorViewModel, if the field contains a color view model, otherwise null

Remarks

This method uses the GetField(string) method to retrieve the field and then attempts to get its value converted to the specified type. If the field does not exist or the value is not of the expected type, null is returned.

GetDateTime(string)

Returns the date and time data value of the specified field.

public DateTime GetDateTime(string systemName)

Parameters

systemName string

The field system name.

Returns

DateTime

The date and time data value of the specified field. Fallback is DateTime.MinValue

Remarks

This method uses the GetField(string) method to retrieve the field and then attempts to get its value converted to the date and time data type. If the field does not exist, DateTime.MinValue is returned.

GetDecimal(string)

Returns the decimal value of the specified field.

public decimal GetDecimal(string systemName)

Parameters

systemName string

The field system name.

Returns

decimal

The decimal value of the specified field. Fallback is 0

Remarks

This method uses the GetField(string) method to retrieve the field and then attempts to get its value converted to the decimal type. If the field does not exist, 0 is returned.

GetDouble(string)

Returns the double value of the specified field.

public double GetDouble(string systemName)

Parameters

systemName string

The field system name.

Returns

double

The double value of the specified field. Fallback is 0

Remarks

This method uses the GetField(string) method to retrieve the field and then attempts to get its value converted to the double type. If the field does not exist, 0 is returned.

GetField(string)

Returns the view model of the field.

public ItemFieldViewModel GetField(string systemName)

Parameters

systemName string

The field system name.

Returns

ItemFieldViewModel

See ItemFieldViewModel

GetFile(string)

Returns a file view model of the field.

public FileViewModel GetFile(string systemName)

Parameters

systemName string

The field system name.

Returns

FileViewModel

See FileViewModel

Remarks

This method uses the GetField(string) method to retrieve the field and then attempts to get its value converted to the specified type. If the field does not exist or the value is not of the expected type, null is returned.

GetFiles(string)

Returns a list of file view models of the field.

public IList<FileViewModel> GetFiles(string systemName)

Parameters

systemName string

The field system name.

Returns

IList<FileViewModel>

See IList<T>

Remarks

This method uses the GetField(string) method to retrieve the field and then attempts to get its value converted to the specified type. If the field does not exist or the value is not of the expected type, null is returned.

GetGeolocation(string)

Returns a geolocation view mode of the field.

public GeolocationViewModel GetGeolocation(string systemName)

Parameters

systemName string

The field system name.

Returns

GeolocationViewModel

See GeolocationViewModel

Remarks

This method uses the GetField(string) method to retrieve the field and then attempts to get its value converted to the specified type. If the field does not exist or the value is not of the expected type, null is returned.

GetGoogleFont(string)

Returns a google font view model of the field.

public GoogleFontViewModel GetGoogleFont(string systemName)

Parameters

systemName string

The field system name.

Returns

GoogleFontViewModel

See GoogleFontViewModel

Remarks

This method uses the GetField(string) method to retrieve the field and then attempts to get its value converted to the specified type. If the field does not exist or the value is not of the expected type, null is returned.

GetInt32(string)

Returns the 32-bit integer value of the field.

public int GetInt32(string systemName)

Parameters

systemName string

The field system name.

Returns

int

The 32-bit integer value of the specified field. Fallback is 0

Remarks

This method uses the GetField(string) method to retrieve the field and then attempts to get its value converted to the 32-bit integer type. If the field does not exist, 0 is returned.

GetInt64(string)

Returns the 64-bit integer value of the field.

public long GetInt64(string systemName)

Parameters

systemName string

The field system name.

Returns

long

The 64-bit integer value of the specified field. Fallback is 0

Remarks

This method uses the GetField(string) method to retrieve the field and then attempts to get its value converted to the 64-bit integer type. If the field does not exist, 0 is returned.

GetItem(string)

Returns an item view model of the field.

public ItemViewModel GetItem(string systemName)

Parameters

systemName string

The field system name.

Returns

ItemViewModel

See ItemViewModel

Remarks

This method uses the GetField(string) method to retrieve the field and then attempts to get its value converted to the specified type. If the field does not exist or the value is not of the expected type, null is returned.

The field's editor type should be the item editor.

GetItems(string)

Returns a list of item view model of the field.

public IList<ItemViewModel> GetItems(string systemName)

Parameters

systemName string

The field system name.

Returns

IList<ItemViewModel>

See IList<T>

Remarks

This method uses the GetField(string) method to retrieve the field and then attempts to get its value converted to the specified type. If the field does not exist or the value is not of the expected type, null is returned.

The field's editor type should be the item relation list editor.

Returns a link view model of the field.

public LinkViewModel GetLink(string systemName)

Parameters

systemName string

The field system name.

Returns

LinkViewModel

See LinkViewModel

This method uses the GetField(string) method to retrieve the field and then attempts to get its value converted to the specified type. If the field does not exist or the value is not of the expected type, null is returned.

GetList(string)

Returns a list view model of the field.

public ListViewModel GetList(string systemName)

Parameters

systemName string

The field system name.

Returns

ListViewModel

See ListViewModel

Remarks

This method uses the GetField(string) method to retrieve the field and then attempts to get its value converted to the specified type. If the field does not exist or the value is not of the expected type, null is returned.

GetRawValue(string)

Returns the raw unmodified value from the database without any parsing

public object GetRawValue(string systemName)

Parameters

systemName string

The field system name.

Returns

object

An object of the values underlying database type. Int, String, Boolean etc. Never another view model or object

GetRawValueString(string)

Returns the raw unmodified value from the database without any parsing but converted to string

public string GetRawValueString(string systemName)

Parameters

systemName string

The field system name.

Returns

string

A string of the underlying raw value.

GetRawValueString(string, string)

Returns the raw unmodified value from the database without any parsing but converted to string. Returns the default value if the field value is null or empty.

public string GetRawValueString(string systemName, string defaultValue)

Parameters

systemName string

The field system name.

defaultValue string

Value to return if the field does not return any value

Returns

string

A string of the underlying raw value.

GetString(string)

Returns the value of the field as string.

public string GetString(string systemName)

Parameters

systemName string

The field system name.

Returns

string

The string value of the specified field.

Remarks

ToString is called on whatever object is returned on the field

GetString(string, string)

Returns the value of the field as string. Returns the default value if the field value is null or empty.

public string GetString(string systemName, string defaultValue)

Parameters

systemName string

The field system name.

defaultValue string

Value to return if the field is null or empty

Returns

string

A string of the underlying raw value.

Remarks

This method uses the GetString(string) method to retrieve the value of the field.

GetUser(string)

Gets a UserViewModel of the field.

public UserViewModel GetUser(string systemName)

Parameters

systemName string

The field system name.

Returns

UserViewModel

See UserViewModel.

GetUserGroup(string)

Gets a UserGroupViewModel of the field.

public UserGroupViewModel GetUserGroup(string systemName)

Parameters

systemName string

The field system name.

Returns

UserGroupViewModel

See UserGroupViewModel.

GetUserGroups(string)

Gets a collection of UserGroupViewModel of the field.

public IList<UserGroupViewModel> GetUserGroups(string systemName)

Parameters

systemName string

The field system name.

Returns

IList<UserGroupViewModel>

A collection of UserGroupViewModel.

GetUsers(string)

Returns a list of user view models of the field.

public IList<UserViewModel> GetUsers(string systemName)

Parameters

systemName string

The field system name.

Returns

IList<UserViewModel>

See IList<T>

Remarks

This method uses the GetField(string) method to retrieve the field and then attempts to get its value converted to the specified type. If the field does not exist or the value is not of the expected type, null is returned.

GetValue(string)

Returns the value of the field.

public object GetValue(string systemName)

Parameters

systemName string

The field system name.

Returns

object

System.Object; The instance of the object would be one of the item view model types

Remarks

This method uses the GetField(string) method to retrieve the field and then attempts to get its value. If the field does not exist null is returned.

GetValue<T>(string)

Returns the value of the field.

public T GetValue<T>(string systemName) where T : class

Parameters

systemName string

The field system name.

Returns

T

The value of the specified field as type T, or null if the field is not found or the value cannot be converted to type T.

Type Parameters

T

The type to which the value should be converted. This type is one of the item field view models.

Remarks

This method uses the GetField(string) method to retrieve the field and then attempts to get its value converted to the specified type. If the field does not exist or the value is not of the expected type, null is returned.

GetVideo(string)

Gets a video view model if the field is a media field

public VideoViewModel GetVideo(string systemName)

Parameters

systemName string

The field system name.

Returns

VideoViewModel

See VideoViewModel

TryGetButton(string, out ButtonViewModel)

Using the systemName to retrieve an item field using GetButton(string). If GetButton(string) returns null, the methods returns false, otherwise returning true.

public bool TryGetButton(string systemName, out ButtonViewModel button)

Parameters

systemName string

Used to look up the corresponding ButtonViewModel if it exists on the item.

button ButtonViewModel

The outButtonViewModel value from the method.

Returns

bool

true if button is not null, otherwise false

TryGetColor(string, out ColorViewModel)

Using the systemName to retrieve an item field using GetColor(string). If GetColor(string) returns null, the methods returns false, otherwise returning true.

public bool TryGetColor(string systemName, out ColorViewModel color)

Parameters

systemName string

The field system name.

color ColorViewModel

When this method returns, contains the ColorViewModel value, if the field is found; otherwise, null.

Returns

bool

true if color is not null, otherwise false

Remarks

This method uses the GetColor(string) method to retrieve the value of the field.

TryGetDateTime(string, out DateTime)

Returns true if the value of the field is specified and a date time is assigned to the passed out parameter.

public bool TryGetDateTime(string systemName, out DateTime dateTime)

Parameters

systemName string

The field system name.

dateTime DateTime

When this method returns, contains the System.DateTime value equivalent of the field value, if the field exists, otherwise DateTime.MinValue.

Returns

bool

true if the field value was converted successfully; otherwise, false

Remarks

This method uses the GetDateTime(string) method to retrieve the value of the field. If the field does not exist or the value cannot be converted to a DateTime, false is returned and DateTime.MinValue is assigned to the dateTime parameter.

TryGetField(string, out ItemFieldViewModel)

Returns true if the value of the field is specified and a item field view model is assigned to the passed out parameter.

public bool TryGetField(string systemName, out ItemFieldViewModel itemField)

Parameters

systemName string

The field system name.

itemField ItemFieldViewModel

When this method returns, contains the ItemFieldViewModel value, if the field exists, otherwise null.

Returns

bool

true if itemField is not null, otherwise false

Remarks

This method uses the GetField(string) method to retrieve the value of the field.

TryGetFile(string, out FileViewModel)

Returns true if the value of the field is specified and a file view model is assigned to the passed out parameter.

public bool TryGetFile(string systemName, out FileViewModel file)

Parameters

systemName string

The field system name.

file FileViewModel

When this method returns, contains the FileViewModel value, if the field exists, otherwise null.

Returns

bool

true if file is not null, otherwise false

Remarks

This method uses the GetFile(string) method to retrieve the value of the field.

TryGetFiles(string, out IList<FileViewModel>)

Returns true if the value of the field is specified and a list of file view models is assigned to the passed out parameter.

public bool TryGetFiles(string systemName, out IList<FileViewModel> files)

Parameters

systemName string

The field system name.

files IList<FileViewModel>

When this method returns, contains the IList<T> value, if the field exists, otherwise null.

Returns

bool

true if files is not null, otherwise false

Remarks

This method uses the GetFiles(string) method to retrieve the value of the field.

TryGetGeolocation(string, out GeolocationViewModel)

Returns true if the value of the field is specified and a geolocation view model is assigned to the passed out parameter.

public bool TryGetGeolocation(string systemName, out GeolocationViewModel geolocation)

Parameters

systemName string

The field system name.

geolocation GeolocationViewModel

When this method returns, contains the GeolocationViewModel value, if the field exists, otherwise null.

Returns

bool

true if geolocation is not null, otherwise false

Remarks

This method uses the GetGeolocation(string) method to retrieve the value of the field.

TryGetGoogleFont(string, out GoogleFontViewModel)

Returns true if the value of the field is specified and a google font view model is assigned to the passed out parameter.

public bool TryGetGoogleFont(string systemName, out GoogleFontViewModel googleFont)

Parameters

systemName string

The field system name.

googleFont GoogleFontViewModel

When this method returns, contains the GoogleFontViewModel value, if the field exists, otherwise null.

Returns

bool

true if googleFont is not null, otherwise false

Remarks

This method uses the GetGoogleFont(string) method to retrieve the value of the field.

TryGetImageFile(string, out ImageFileViewModel)

Returns true if the value of the field is specified and an imagefile view model is assigned to the passed out parameter.

public bool TryGetImageFile(string systemName, out ImageFileViewModel image)

Parameters

systemName string

The field system name.

image ImageFileViewModel

When this method returns, contains the ImageFileViewModel value, if the field exists, otherwise null.

Returns

bool

true if image is not null, otherwise false

Remarks

This method uses the GetField(string) method to retrieve the field and then attempts to get its value converted to the specified type.

TryGetImageFiles(string, out IEnumerable<ImageFileViewModel>)

Returns true if the value of the field is specified and a list of imagefile view models is assigned to the passed out parameter.

public bool TryGetImageFiles(string systemName, out IEnumerable<ImageFileViewModel> images)

Parameters

systemName string

The field system name.

images IEnumerable<ImageFileViewModel>

When this method returns, contains the IEnumerable<T> value, if the field exists, otherwise null.

Returns

bool

true if images is not null, otherwise false

Remarks

This method uses the GetField(string) method to retrieve the field and then attempts to get its value converted to the specified type.

TryGetItem(string, out ItemViewModel)

Returns true if the value of the field is specified and an item view model is assigned to the passed out parameter.

public bool TryGetItem(string systemName, out ItemViewModel item)

Parameters

systemName string

The field system name.

item ItemViewModel

When this method returns, contains the ItemViewModel value, if the field exists, otherwise null.

Returns

bool

true if item is not null, otherwise false

Remarks

This method uses the GetItem(string) method to retrieve the value of the field.

TryGetItems(string, out IList<ItemViewModel>)

Returns true if the value of the field is specified and a list of item view models is assigned to the passed out parameter.

public bool TryGetItems(string systemName, out IList<ItemViewModel> items)

Parameters

systemName string

The field system name.

items IList<ItemViewModel>

When this method returns, contains the IList<T> value, if the field exists, otherwise null.

Returns

bool

true if items is not null, otherwise false

Remarks

This method uses the GetItems(string) method to retrieve the value of the field.

Returns true if the value of the field is specified and a link view model is assigned to the passed out parameter.

public bool TryGetLink(string systemName, out LinkViewModel link)

Parameters

systemName string

The field system name.

link LinkViewModel

When this method returns, contains the LinkViewModel value, if the field is found; otherwise, null.

Returns

bool

true if link is not null, otherwise false

This method uses the GetLink(string) method to retrieve the value of the field.

TryGetList(string, out ListViewModel)

Returns true if the value of the field is specified and a list view model is assigned to the passed out parameter.

public bool TryGetList(string systemName, out ListViewModel list)

Parameters

systemName string

The field system name.

list ListViewModel

When this method returns, contains the ListViewModel value, if the field is found; otherwise, null.

Returns

bool

true if list is not null, otherwise false

Remarks

This method uses the GetList(string) method to retrieve the value of the field.

TryGetString(string, out string)

Returns true if the value of the field is specified and a string is assigned to the passed out parameter.

public bool TryGetString(string systemName, out string value)

Parameters

systemName string

The field system name.

value string

When this method returns, contains the string value equivalent of the field value, if the field exists, otherwise null.

Returns

bool

true if value is not null and not empty, otherwise false

Remarks

This method uses the GetString(string) method to retrieve the value of the field.

TryGetUser(string, out UserViewModel)

Using the systemName to retrieve an item field using GetUser(string). If GetUser(string) returns null, the method returns false, otherwise true.

public bool TryGetUser(string systemName, out UserViewModel user)

Parameters

systemName string

Used to look up the corresponding UserViewModel if it exists on the item.

user UserViewModel

The outUserViewModel value from the method.

Returns

bool

true if user is not null, otherwise false.

TryGetUserGroup(string, out UserGroupViewModel)

Using the systemName to retrieve an item field using GetUserGroup(string). If GetUserGroup(string) returns null, the method returns false, otherwise true.

public bool TryGetUserGroup(string systemName, out UserGroupViewModel userGroup)

Parameters

systemName string

Used to look up the corresponding UserGroupViewModel if it exists on the item.

userGroup UserGroupViewModel

The outUserGroupViewModel value from the method.

Returns

bool

true if userGroup is not null, otherwise false.

TryGetUserGroups(string, out IList<UserGroupViewModel>)

Using the systemName to retrieve an item field using GetUserGroups(string). If GetUserGroups(string) returns null or an empty collection, the method returns false, otherwise true.

public bool TryGetUserGroups(string systemName, out IList<UserGroupViewModel> userGroups)

Parameters

systemName string

Used to look up the corresponding UserGroupViewModel if it exists on the item.

userGroups IList<UserGroupViewModel>

The outUserGroupViewModel value from the method.

Returns

bool

true if userGroups is not null and has elements, otherwise false.

TryGetUsers(string, out IList<UserViewModel>)

Returns true if the value of the field is specified and a list of user view models is assigned to the passed out parameter.

public bool TryGetUsers(string systemName, out IList<UserViewModel> users)

Parameters

systemName string

The field system name.

users IList<UserViewModel>

When this method returns, contains the IList<T> value, if the field is found; otherwise, null.

Returns

bool

true if users is not null, otherwise false

Remarks

This method uses the GetUsers(string) method to retrieve the value of the field.

TryGetValue<T>(string, out T)

Returns true if the value of the field is specified and some value of type T is assigned to the passed out parameter.

public bool TryGetValue<T>(string systemName, out T value) where T : class

Parameters

systemName string

The field system name.

value T

When this method returns, contains the value of type T, if the field is found; otherwise, null.

Returns

bool

true if value is not null, otherwise false

Type Parameters

T

The type to which the value should be converted. This type is one of the item field view models.

Remarks

This method uses the GetValue<T>(string) method to retrieve the value of the field.

TryGetVideo(string, out VideoViewModel)

Using the systemName to retrieve an item field using GetVideo(string). If GetVideo(string) returns null, the methods returns false, otherwise returning true.

public bool TryGetVideo(string systemName, out VideoViewModel video)

Parameters

systemName string

Used to look up the corresponding VideoViewModel if it exists on the item.

video VideoViewModel

The outVideoViewModel value from the method.

Returns

bool

true if video is not null, otherwise false

See Also

To top