Table of Contents

Interface ITypedModel

Namespace
ToSic.Sxc.Code
Assembly
ToSic.Sxc.dll

Object in partial Razor files to access parameters handed in. Example caller:

@Html.Partial(someFile, new { blogPost, file = mainFile, title = "hello" }

Example partial:

var blogPost = MyModel.Item("BlogPost");
var file = MyModel.File("File");
var title = MyModel.String("Title");
Tip

The common data types such as string or ITypedItem have methods to quickly get them in the desired type. This allows things such as var message = MyModel.String("Message"); For less common types you'll need to use Get(string, NoParamOrder, bool?) and cast it as needed, like this: string message = MyModel.Get("Message");.

[PublicApi]
public interface ITypedModel

Remarks

Introduced in v16.02

Methods

Bool(string, NoParamOrder, bool?, bool?)

Will get the value and return in the desired type.

bool Bool(string name, NoParamOrder protector = default, bool? fallback = null, bool? required = null)

Parameters

name string

Property name on the passed in data object

protector NoParamOrder

see Convention: Named Parameters

fallback bool?

The optional fallback value.

required bool?

Throw error if name doesn't exist, see Convention: Property Required Name Checks. It is automatically false if a fallback is set / not null.

Returns

bool

typed result if found, false if not found.

Code(string, NoParamOrder, object, bool?)

Get code forwarded to the current razor. Code was usually created in the caller using GetCode(...) and may need to be passed around.

dynamic Code(string name, NoParamOrder protector = default, object fallback = null, bool? required = null)

Parameters

name string

Property name on the passed in data object

protector NoParamOrder

see Convention: Named Parameters

fallback object

A fallback to use if not found - not commonly used here.

required bool?

Throw error if name doesn't exist, see Convention: Property Required Name Checks. It is automatically false if a fallback is not null.

Returns

dynamic

The resulting object is dynamic which is necessary for making calls to methods etc.

Remarks

New in 16.05

ContainsKey(string)

Check if this typed object has a property of this specified name. It's case-insensitive.

bool ContainsKey(string name)

Parameters

name string

the name like Image; some objects also support path to sub-property like Author.Name

Returns

bool

Remarks

Adding in 16.03 (WIP)

DateTime(string, NoParamOrder, DateTime?, bool?)

Will get the value and return in the desired type.

DateTime DateTime(string name, NoParamOrder protector = default, DateTime? fallback = null, bool? required = null)

Parameters

name string

Property name on the passed in data object

protector NoParamOrder

see Convention: Named Parameters

fallback DateTime?

The optional fallback value.

required bool?

Throw error if name doesn't exist, see Convention: Property Required Name Checks. It is automatically false if a fallback is set / not null.

Returns

DateTime

typed result if found, default-date if not found.

Decimal(string, NoParamOrder, decimal?, bool?)

Will get the value and return in the desired type.

decimal Decimal(string name, NoParamOrder protector = default, decimal? fallback = null, bool? required = null)

Parameters

name string

Property name on the passed in data object

protector NoParamOrder

see Convention: Named Parameters

fallback decimal?

The optional fallback value.

required bool?

Throw error if name doesn't exist, see Convention: Property Required Name Checks. It is automatically false if a fallback is not null.

Returns

decimal

int result if found, 0 if not found/convertible.

Exceptions

ArgumentException

if the name is not found and no fallback provided and required not false

Double(string, NoParamOrder, double?, bool?)

Will get the value and return in the desired type.

double Double(string name, NoParamOrder protector = default, double? fallback = null, bool? required = null)

Parameters

name string

Property name on the passed in data object

protector NoParamOrder

see Convention: Named Parameters

fallback double?

The optional fallback value.

required bool?

Throw error if name doesn't exist, see Convention: Property Required Name Checks. It is automatically false if a fallback is not null.

Returns

double

int result if found, 0 if not found/convertible.

Exceptions

ArgumentException

if the name is not found and no fallback provided and required not false

File(string, NoParamOrder, IFile, bool?)

Will get the value if specified. If the value is a list of files, then this will only return the first one.

IFile File(string name, NoParamOrder protector = default, IFile fallback = null, bool? required = null)

Parameters

name string

Property name on the passed in data object

protector NoParamOrder

see Convention: Named Parameters

fallback IFile

The optional fallback value.

required bool?

Throw error if name doesn't exist, see Convention: Property Required Name Checks. It is automatically false if a fallback is set / not null.

Returns

IFile

typed result if found, null if not found.

Files(string, NoParamOrder, IEnumerable<IFile>, bool?)

Will get the value if specified. If the value is a single file, will return a list containing that file.

IEnumerable<IFile> Files(string name, NoParamOrder protector = default, IEnumerable<IFile> fallback = null, bool? required = null)

Parameters

name string

Property name on the passed in data object

protector NoParamOrder

see Convention: Named Parameters

fallback IEnumerable<IFile>

The optional fallback value.

required bool?

Throw error if name doesn't exist, see Convention: Property Required Name Checks. It is automatically false if a fallback is set / not null.

Returns

IEnumerable<IFile>

typed result if found, empty-list if not found.

Float(string, NoParamOrder, float?, bool?)

Will get the value and return in the desired type.

float Float(string name, NoParamOrder protector = default, float? fallback = null, bool? required = null)

Parameters

name string

Property name on the passed in data object

protector NoParamOrder

see Convention: Named Parameters

fallback float?

The optional fallback value.

required bool?

Throw error if name doesn't exist, see Convention: Property Required Name Checks. It is automatically false if a fallback is not null.

Returns

float

int result if found, 0 if not found/convertible.

Exceptions

ArgumentException

if the name is not found and no fallback provided and required not false

Folder(string, NoParamOrder, IFolder, bool?)

Will get the value if specified. If the value is a list of folders, then this will only return the first one.

IFolder Folder(string name, NoParamOrder protector = default, IFolder fallback = null, bool? required = null)

Parameters

name string

Property name on the passed in data object

protector NoParamOrder

see Convention: Named Parameters

fallback IFolder

The optional fallback value.

required bool?

Throw error if name doesn't exist, see Convention: Property Required Name Checks. It is automatically false if a fallback is set / not null.

Returns

IFolder

typed result if found, null if not found.

Folders(string, NoParamOrder, IEnumerable<IFolder>, bool?)

Will get the value if specified. If the value is a single folder, will return a list containing that folder.

IEnumerable<IFolder> Folders(string name, NoParamOrder protector = default, IEnumerable<IFolder> fallback = null, bool? required = null)

Parameters

name string

Property name on the passed in data object

protector NoParamOrder

see Convention: Named Parameters

fallback IEnumerable<IFolder>

The optional fallback value.

required bool?

Throw error if name doesn't exist, see Convention: Property Required Name Checks. It is automatically false if a fallback is set / not null.

Returns

IEnumerable<IFolder>

typed result if found, empty-list if not found.

Get(string, NoParamOrder, bool?)

Will get the value and return as object, since the type isn't known.

object Get(string name, NoParamOrder protector = default, bool? required = null)

Parameters

name string

Property name on the passed in data object

protector NoParamOrder

see Convention: Named Parameters

required bool?

throw error if name doesn't exist, see Convention: Property Required Name Checks

Returns

object

Object if found, null if not found.

Get<T>(string, NoParamOrder, T, bool?)

Will get the value and return as type T as specified.

T Get<T>(string name, NoParamOrder protector = default, T fallback = default, bool? required = null)

Parameters

name string

Property name on the passed in data object

protector NoParamOrder

see Convention: Named Parameters

fallback T

The fallback value. If provided, the type is automatically determined.

required bool?

Throw error if name doesn't exist, see Convention: Property Required Name Checks. It is automatically false if a fallback is provided which is not the default. So like Get<string>(..., fallback: false) can't be detected, but ..., fallback: "hello" can.

Returns

T

Object of type T if found, null if not found.

Type Parameters

T

The returned type

Gps(string, NoParamOrder, GpsCoordinates, bool?)

Will get the value and return in the desired type.

GpsCoordinates Gps(string name, NoParamOrder protector = default, GpsCoordinates fallback = null, bool? required = null)

Parameters

name string

Property name on the passed in data object

protector NoParamOrder

see Convention: Named Parameters

fallback GpsCoordinates

The optional fallback value.

required bool?

Throw error if name doesn't exist, see Convention: Property Required Name Checks. It is automatically false if a fallback is not null.

Returns

GpsCoordinates

GpsCoordinates if found, null if not found/convertible.

Remarks

Added in 17.08

Exceptions

ArgumentException

if the name is not found and no fallback provided and required not false

Guid(string, NoParamOrder, Guid?, bool?)

Will get the value and return in the desired type.

Guid Guid(string name, NoParamOrder protector = default, Guid? fallback = null, bool? required = null)

Parameters

name string

Property name on the passed in data object

protector NoParamOrder

see Convention: Named Parameters

fallback Guid?

The optional fallback value.

required bool?

Throw error if name doesn't exist, see Convention: Property Required Name Checks. It is automatically false if a fallback is set / not null.

Returns

Guid

typed result if found, empty-guid if not found.

HtmlTag(string, NoParamOrder, IHtmlTag, bool?)

Will get the value being an IHtmlTag as specified (RazorBlade objects)

IHtmlTag HtmlTag(string name, NoParamOrder protector = default, IHtmlTag fallback = null, bool? required = null)

Parameters

name string

Property name on the passed in data object

protector NoParamOrder

see Convention: Named Parameters

fallback IHtmlTag

The optional fallback value.

required bool?

Throw error if name doesn't exist, see Convention: Property Required Name Checks. It is automatically false if a fallback is not null.

Returns

IHtmlTag

typed result if found, null if not found

HtmlTags(string, NoParamOrder, IEnumerable<IHtmlTag>, bool?)

Will get the value being an list (IEnumerable) of IHtmlTag as specified (RazorBlade objects)

IEnumerable<IHtmlTag> HtmlTags(string name, NoParamOrder protector = default, IEnumerable<IHtmlTag> fallback = null, bool? required = null)

Parameters

name string

Property name on the passed in data object

protector NoParamOrder

see Convention: Named Parameters

fallback IEnumerable<IHtmlTag>

The optional fallback value.

required bool?

Throw error if name doesn't exist, see Convention: Property Required Name Checks. It is automatically false if a fallback is not null.

Returns

IEnumerable<IHtmlTag>

typed result if found, null if not found

Int(string, NoParamOrder, int?, bool?)

Will get the value and return in the desired type.

int Int(string name, NoParamOrder protector = default, int? fallback = null, bool? required = null)

Parameters

name string

Property name on the passed in data object

protector NoParamOrder

see Convention: Named Parameters

fallback int?

The optional fallback value.

required bool?

Throw error if name doesn't exist, see Convention: Property Required Name Checks. It is automatically false if a fallback is not null.

Returns

int

int result if found, 0 if not found/convertible.

Exceptions

ArgumentException

if the name is not found and no fallback provided and required not false

Item(string, NoParamOrder, ITypedItem, bool?)

Will get the value if specified. If the value is a list of items, then this will only return the first one.

ITypedItem Item(string name, NoParamOrder protector = default, ITypedItem fallback = null, bool? required = null)

Parameters

name string

Property name on the passed in data object

protector NoParamOrder

see Convention: Named Parameters

fallback ITypedItem

The optional fallback value.

required bool?

Throw error if name doesn't exist, see Convention: Property Required Name Checks. It is automatically false if a fallback is set / not null.

Returns

ITypedItem

typed result if found, null if not found.

Items(string, NoParamOrder, IEnumerable<ITypedItem>, bool?)

Will get the value if specified. If the value is a single item, will return a list containing that item.

IEnumerable<ITypedItem> Items(string name, NoParamOrder protector = default, IEnumerable<ITypedItem> fallback = null, bool? required = null)

Parameters

name string

Property name on the passed in data object

protector NoParamOrder

see Convention: Named Parameters

fallback IEnumerable<ITypedItem>

The optional fallback value.

required bool?

Throw error if name doesn't exist, see Convention: Property Required Name Checks. It is automatically false if a fallback is set / not null.

Returns

IEnumerable<ITypedItem>

typed result if found, empty-list if not found.

Keys(NoParamOrder, IEnumerable<string>)

Get all the keys available in this Model (all the parameters passed in). This is used to sometimes run early checks if all the expected parameters have been provided.

IEnumerable<string> Keys(NoParamOrder protector = default, IEnumerable<string> only = null)

Parameters

protector NoParamOrder
only IEnumerable<string>

Only return the keys specified here, if found. Typical use: only: new [] { "Key1", "Key2" }. Useful to check if all or any specific keys exist.

Returns

IEnumerable<string>

Remarks

Added in 16.03

String(string, NoParamOrder, string, bool?)

Will get the value and return in the desired type.

string String(string name, NoParamOrder protector = default, string fallback = null, bool? required = null)

Parameters

name string

Property name on the passed in data object

protector NoParamOrder

see Convention: Named Parameters

fallback string

The optional fallback value.

required bool?

Throw error if name doesn't exist, see Convention: Property Required Name Checks. It is automatically false if a fallback is not null.

Returns

string

typed result if found, null if not found.

Toolbar(string, NoParamOrder, IToolbarBuilder, bool?)

Will get the value being a toolbar as specified.

IToolbarBuilder Toolbar(string name, NoParamOrder protector = default, IToolbarBuilder fallback = null, bool? required = null)

Parameters

name string

Property name on the passed in data object

protector NoParamOrder

see Convention: Named Parameters

fallback IToolbarBuilder

The optional fallback value.

required bool?

Throw error if name doesn't exist, see Convention: Property Required Name Checks. It is automatically false if a fallback is set / not null.

Returns

IToolbarBuilder

typed result if found, null if not found