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 aTableRecordobject 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>
QueryParameterNameandTableName— 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>
RecordIdandTableName— 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.
RecordContext Class
Parameters
Name | Type | Default | Description |
|---|---|---|---|
ChildContent | RenderFragment? | The child components rendered within this context. | |
ContextTemplate | RenderFragment<TableRecord> | You can optionaly specifiy a template for record. This allows you access to the 'TableRecord' object in the template. | |
DisableUnsavedChangesWarning | bool? | 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. | |
ForceSuccessfulValidationBeforeSave | bool | True | Should a successful validation of the record be performed before allowing the record to be saved. |
IsDirty | bool | False | Indicates whether the record or any registered child context has unsaved changes. |
ParentContext | MainContext? | The nearest ancestor MainContext, enabling nested context hierarchies. | |
QueryParameterName | string? | The name of the query parameter which specifies the id of the record to retrieve. | |
Record | TableRecord? | The record linked to this record context. | |
RecordId | string? | The specific GUID of the record for the context. | |
TableName | string? | The table logical name of the record. |
ChildContentContextTemplateDisableUnsavedChangesWarningThe warning is always shown when refreshing, regardless of this setting.
If not explicitly set, the value is inherited from the parent context.
ForceSuccessfulValidationBeforeSaveIsDirtyParentContextQueryParameterNameRecordRecordIdTableNameEvents
Name | Type | Description |
|---|---|---|
OnBeforeDelete | EventCallback<CancelEventArgs> | Callback called before deleting. Allows for cancelling the delete operation. |
OnBeforeSave | EventCallback<CancelEventArgs> | Callback called before saving. Allows for cancelling the save operation. |
RecordLoaded | EventCallback<TableRecord> | EventCallback to trigger when the record is loaded. |
OnBeforeDeleteOnBeforeSaveRecordLoadedMethods
Name | Parameters | Type | Description |
|---|---|---|---|
DeleteAsync | Task<bool> | Prompts for confirmation then deletes the current record. | |
GetRequests | List<OrganizationRequest> | Returns the create or update request for the record if it is dirty, plus requests from child contexts. | |
RefreshAsync | bool forceRefresh | Task | Reloads the record from the server, optionally prompting about unsaved changes. |
ResetState | void | Resets the record and all child contexts to their last saved state. | |
SaveAsync | bool? refresh | Task<bool> | Saves the record and navigates to the record URL if it was newly created. |
Validate | bool | Validate the record and any sub-contexts (grid, lookup edit, record, main). |
DeleteAsyncGetRequestsRefreshAsyncResetStateSaveAsyncValidate