RecordContext

Here is an example that demonstrates the use of a RecordContext component.

Loading a Record

There are several ways to specify which record a RecordContext should load:

  • Record — Pass a TableRecord object directly. Useful when you already have the record in memory or are creating a new record.
<RecordContext Record="@(new TableRecord { TableName = "contact" })">
    <TextEdit ColumnName="firstname" />
    <TextEdit ColumnName="lastname" />
</RecordContext>
  • QueryParameterName and TableName — The context reads the record ID from a URL query parameter and retrieves the record from Dataverse automatically. If no ID is present, a new record is created.
<!-- URL: /my-page?id=00000000-0000-0000-0000-000000000001 -->
<RecordContext TableName="contact" QueryParameterName="id">
    <TextEdit ColumnName="firstname" />
    <TextEdit ColumnName="lastname" />
</RecordContext>
  • RecordId and TableName — Specify the record ID directly as a parameter instead of reading it from the URL.
<RecordContext TableName="contact" RecordId="00000000-0000-0000-0000-000000000001">
    <TextEdit ColumnName="firstname" />
</RecordContext>

Record Loaded Event

Use the RecordLoaded callback to run logic after the record has been retrieved from Dataverse. This is useful for initializing dependent state or loading related data.

<RecordContext TableName="contact"
              QueryParameterName="id"
              RecordLoaded="OnRecordLoaded">
    <TextEdit ColumnName="firstname" />
</RecordContext>

@code {
    private async Task OnRecordLoaded(TableRecord record)
    {
        // Initialize dependent state or load related data
    }
}

Context Template

Instead of using ChildContent, you can use the ContextTemplate parameter to access the TableRecord object directly in your markup via a template variable.

<RecordContext TableName="contact" QueryParameterName="id">
    <ContextTemplate>
        <h2>Editing: @context["firstname"]</h2>
        <TextEdit ColumnName="firstname" />
        <TextEdit ColumnName="lastname" />
    </ContextTemplate>
</RecordContext>

Deleting a Record

Call DeleteAsync() to delete the current record. The user is prompted with a confirmation dialog before the delete is executed. Use the OnBeforeDelete callback to run custom logic or cancel the delete by setting CancelEventArgs.Cancel to true.

<RecordContext TableName="contact"
              QueryParameterName="id"
              OnBeforeDelete="OnBeforeDelete">
    <MenuBar>
        <DeleteContextButton />
    </MenuBar>
    <TextEdit ColumnName="firstname" />
</RecordContext>

@code {
    private async Task OnBeforeDelete(CancelEventArgs eventArgs)
    {
        // Optionally cancel the delete
        // eventArgs.Cancel = true;
    }
}

URL Navigation

When using QueryParameterName, the RecordContext automatically monitors URL changes. If the query parameter value changes while staying on the same page, the context reloads the new record without a full page navigation. After saving a newly created record, the URL is updated to include the new record's ID.

Example

The following example demonstrates a simple RecordContext that displays text for the current record.

Example
RedBlueOrangePurpleYellowBlackWhiteBrownGreen

RecordContext Class

Parameters

Name
Type
Default
Description
ChildContentRenderFragment?
The child components rendered within this context.
ContextTemplateRenderFragment<TableRecord>
You can optionaly specifiy a template for record. This allows you access to the 'TableRecord' object in the template.
DisableUnsavedChangesWarningbool?
When set to true, the unsaved changes warning dialog is not displayed when navigating away from the page.
The warning is always shown when refreshing, regardless of this setting.
If not explicitly set, the value is inherited from the parent context.
ForceSuccessfulValidationBeforeSavebool
True
Should a successful validation of the record be performed before allowing the record to be saved.
IsDirtybool
False
Indicates whether the record or any registered child context has unsaved changes.
ParentContextMainContext?
The nearest ancestor MainContext, enabling nested context hierarchies.
QueryParameterNamestring?
The name of the query parameter which specifies the id of the record to retrieve.
RecordTableRecord?
The record linked to this record context.
RecordIdstring?
The specific GUID of the record for the context.
TableNamestring?
The table logical name of the record.
Name: ChildContent
Type: RenderFragment?
Description: The child components rendered within this context.
Name: ContextTemplate
Type: RenderFragment<TableRecord>
Description: You can optionaly specifiy a template for record. This allows you access to the 'TableRecord' object in the template.
Name: DisableUnsavedChangesWarning
Type: bool?
Description: When set to true, the unsaved changes warning dialog is not displayed when navigating away from the page.
The warning is always shown when refreshing, regardless of this setting.
If not explicitly set, the value is inherited from the parent context.
Name: ForceSuccessfulValidationBeforeSave
Type: bool
Default: True
Description: Should a successful validation of the record be performed before allowing the record to be saved.
Name: IsDirty
Type: bool
Default: False
Description: Indicates whether the record or any registered child context has unsaved changes.
Name: ParentContext
Type: MainContext?
Description: The nearest ancestor MainContext, enabling nested context hierarchies.
Name: QueryParameterName
Type: string?
Description: The name of the query parameter which specifies the id of the record to retrieve.
Name: Record
Type: TableRecord?
Description: The record linked to this record context.
Name: RecordId
Type: string?
Description: The specific GUID of the record for the context.
Name: TableName
Type: string?
Description: The table logical name of the record.

Events

Name
Type
Description
OnBeforeDeleteEventCallback<CancelEventArgs>
Callback called before deleting. Allows for cancelling the delete operation.
OnBeforeSaveEventCallback<CancelEventArgs>
Callback called before saving. Allows for cancelling the save operation.
RecordLoadedEventCallback<TableRecord>
EventCallback to trigger when the record is loaded.
Name: OnBeforeDelete
Type: EventCallback<CancelEventArgs>
Description: Callback called before deleting. Allows for cancelling the delete operation.
Name: OnBeforeSave
Type: EventCallback<CancelEventArgs>
Description: Callback called before saving. Allows for cancelling the save operation.
Name: RecordLoaded
Type: EventCallback<TableRecord>
Description: EventCallback to trigger when the record is loaded.

Methods

Name
Parameters
Type
Description
DeleteAsyncTask<bool>
Prompts for confirmation then deletes the current record.
GetRequestsList<OrganizationRequest>
Returns the create or update request for the record if it is dirty, plus requests from child contexts.
RefreshAsyncbool forceRefresh
Task
Reloads the record from the server, optionally prompting about unsaved changes.
ResetStatevoid
Resets the record and all child contexts to their last saved state.
SaveAsyncbool? refresh
Task<bool>
Saves the record and navigates to the record URL if it was newly created.
Validatebool
Validate the record and any sub-contexts (grid, lookup edit, record, main).
Name: DeleteAsync
Type: Task<bool>
Description: Prompts for confirmation then deletes the current record.
Name: GetRequests
Type: List<OrganizationRequest>
Description: Returns the create or update request for the record if it is dirty, plus requests from child contexts.
Name: RefreshAsync
Parameters: bool forceRefresh
Type: Task
Description: Reloads the record from the server, optionally prompting about unsaved changes.
Name: ResetState
Type: void
Description: Resets the record and all child contexts to their last saved state.
Name: SaveAsync
Parameters: bool? refresh
Type: Task<bool>
Description: Saves the record and navigates to the record URL if it was newly created.
Name: Validate
Type: bool
Description: Validate the record and any sub-contexts (grid, lookup edit, record, main).