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'
Link
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
stringThe field system name.
Returns
- bool
true
orfalse
, which reflects the value stored in specified field. Fallback isfalse
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
stringThe field system name.
Returns
GetColor(string)
Returns a color view model of the field.
public ColorViewModel GetColor(string systemName)
Parameters
systemName
stringThe 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
stringThe 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
stringThe 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
stringThe 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
stringThe field system name.
Returns
GetFile(string)
Returns a file view model of the field.
public FileViewModel GetFile(string systemName)
Parameters
systemName
stringThe field system name.
Returns
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
stringThe 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
stringThe field system name.
Returns
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
stringThe field system name.
Returns
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
stringThe 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
stringThe 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
stringThe field system name.
Returns
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
stringThe 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.
GetLink(string)
Returns a link view model of the field.
public LinkViewModel GetLink(string systemName)
Parameters
systemName
stringThe field system name.
Returns
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.
GetList(string)
Returns a list view model of the field.
public ListViewModel GetList(string systemName)
Parameters
systemName
stringThe field system name.
Returns
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
stringThe 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
stringThe 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
stringThe field system name.
defaultValue
stringValue 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
stringThe 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
stringThe field system name.
defaultValue
stringValue 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
stringThe field system name.
Returns
GetUserGroup(string)
Gets a UserGroupViewModel of the field.
public UserGroupViewModel GetUserGroup(string systemName)
Parameters
systemName
stringThe field system name.
Returns
GetUserGroups(string)
Gets a collection of UserGroupViewModel of the field.
public IList<UserGroupViewModel> GetUserGroups(string systemName)
Parameters
systemName
stringThe 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
stringThe 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
stringThe 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
stringThe 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 typeT
.
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
stringThe field system name.
Returns
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
stringUsed to look up the corresponding ButtonViewModel if it exists on the item.
button
ButtonViewModelThe
out
ButtonViewModel value from the method.
Returns
- bool
true
ifbutton
is not null, otherwisefalse
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
stringThe field system name.
color
ColorViewModelWhen this method returns, contains the ColorViewModel value, if the field is found; otherwise,
null
.
Returns
- bool
true
ifcolor
is not null, otherwisefalse
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
stringThe field system name.
dateTime
DateTimeWhen 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
stringThe field system name.
itemField
ItemFieldViewModelWhen this method returns, contains the ItemFieldViewModel value, if the field exists, otherwise
null
.
Returns
- bool
true
ifitemField
is not null, otherwisefalse
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
stringThe field system name.
file
FileViewModelWhen this method returns, contains the FileViewModel value, if the field exists, otherwise
null
.
Returns
- bool
true
iffile
is not null, otherwisefalse
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
stringThe field system name.
files
IList<FileViewModel>When this method returns, contains the IList<T> value, if the field exists, otherwise
null
.
Returns
- bool
true
iffiles
is not null, otherwisefalse
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
stringThe field system name.
geolocation
GeolocationViewModelWhen this method returns, contains the GeolocationViewModel value, if the field exists, otherwise
null
.
Returns
- bool
true
ifgeolocation
is not null, otherwisefalse
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
stringThe field system name.
googleFont
GoogleFontViewModelWhen this method returns, contains the GoogleFontViewModel value, if the field exists, otherwise
null
.
Returns
- bool
true
ifgoogleFont
is not null, otherwisefalse
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
stringThe field system name.
image
ImageFileViewModelWhen this method returns, contains the ImageFileViewModel value, if the field exists, otherwise
null
.
Returns
- bool
true
ifimage
is not null, otherwisefalse
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
stringThe field system name.
images
IEnumerable<ImageFileViewModel>When this method returns, contains the IEnumerable<T> value, if the field exists, otherwise
null
.
Returns
- bool
true
ifimages
is not null, otherwisefalse
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
stringThe field system name.
item
ItemViewModelWhen this method returns, contains the ItemViewModel value, if the field exists, otherwise
null
.
Returns
- bool
true
ifitem
is not null, otherwisefalse
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
stringThe field system name.
items
IList<ItemViewModel>When this method returns, contains the IList<T> value, if the field exists, otherwise
null
.
Returns
- bool
true
ifitems
is not null, otherwisefalse
Remarks
This method uses the GetItems(string) method to retrieve the value of the field.
TryGetLink(string, out LinkViewModel)
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
stringThe field system name.
link
LinkViewModelWhen this method returns, contains the LinkViewModel value, if the field is found; otherwise,
null
.
Returns
- bool
true
iflink
is not null, otherwisefalse
Remarks
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
stringThe field system name.
list
ListViewModelWhen this method returns, contains the ListViewModel value, if the field is found; otherwise,
null
.
Returns
- bool
true
iflist
is not null, otherwisefalse
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
stringThe field system name.
value
stringWhen this method returns, contains the string value equivalent of the field value, if the field exists, otherwise
null
.
Returns
- bool
true
ifvalue
is not null and not empty, otherwisefalse
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
stringUsed to look up the corresponding UserViewModel if it exists on the item.
user
UserViewModelThe
out
UserViewModel value from the method.
Returns
- bool
true
ifuser
is not null, otherwisefalse
.
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
stringUsed to look up the corresponding UserGroupViewModel if it exists on the item.
userGroup
UserGroupViewModelThe
out
UserGroupViewModel value from the method.
Returns
- bool
true
ifuserGroup
is not null, otherwisefalse
.
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
stringUsed to look up the corresponding UserGroupViewModel if it exists on the item.
userGroups
IList<UserGroupViewModel>The
out
UserGroupViewModel value from the method.
Returns
- bool
true
ifuserGroups
is not null and has elements, otherwisefalse
.
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
stringThe 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
ifusers
is not null, otherwisefalse
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
stringThe field system name.
value
TWhen this method returns, contains the value of type
T
, if the field is found; otherwise,null
.
Returns
- bool
true
ifvalue
is not null, otherwisefalse
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
stringUsed to look up the corresponding VideoViewModel if it exists on the item.
video
VideoViewModelThe
out
VideoViewModel value from the method.
Returns
- bool
true
ifvideo
is not null, otherwisefalse