MainContext

The MainContext component is used to encapsulate one or more RecordContexts, SubGrids, or ManyToManyLookupEditors and coordinates save, refresh, validation, and dirty-state tracking across all of them.

Child Components

A MainContext can contain any combination of the following child components:

  • RecordContext — Wraps a single Dataverse record for editing.
  • SubGrid — Displays and manages a collection of related records.
  • ManyToManyLookupEditor — Manages many-to-many relationships.
<MainContext>
    <RecordContext Record="@_contact">
        <TextEdit ColumnName="firstname" />
        <TextEdit ColumnName="lastname" />
    </RecordContext>

    <RecordContext Record="@_account">
        <TextEdit ColumnName="name" />
    </RecordContext>
</MainContext>

Saving

Include a SaveContextButton inside a MenuBar to allow users to persist changes. When the save button is clicked, all dirty records across all child components are saved in a single transactional request to Dataverse. If any record fails to save, the entire transaction rolls back and no changes are persisted.

<MainContext>
    <MenuBar>
        <SaveContextButton />
        <RefreshContextButton />
    </MenuBar>

    <RecordContext Record="@_contact">
        <TextEdit ColumnName="firstname" />
    </RecordContext>

    <RecordContext Record="@_account">
        <TextEdit ColumnName="name" />
    </RecordContext>
</MainContext>

Refreshing

Include a RefreshContextButton to allow users to discard unsaved changes and reload the records from Dataverse. If there are unsaved changes, the user is prompted with a confirmation dialog before the refresh is performed.

Validation

By default, the MainContext validates all child components before saving. Set ForceSuccessfulValidationBeforeSave to false to disable this behavior.

<MainContext ForceSuccessfulValidationBeforeSave="false">
    <MenuBar>
        <SaveContextButton />
    </MenuBar>

    <RecordContext Record="@_record">
        <TextEdit ColumnName="name" />
    </RecordContext>
</MainContext>

Dirty State Tracking

The MainContext automatically tracks whether any child component has unsaved changes via the IsDirty property. If a user attempts to navigate away from the page while there are unsaved changes, a confirmation dialog is displayed. Set DisableUnsavedChangesWarning to true to suppress this dialog for navigation changes. The warning is always shown when refreshing, regardless of this setting.

<MainContext DisableUnsavedChangesWarning="true">
    <MenuBar>
        <SaveContextButton />
        <RefreshContextButton />
    </MenuBar>

    <RecordContext Record="@_record">
        <TextEdit ColumnName="name" />
    </RecordContext>
</MainContext>

Intercepting Save

Use the OnBeforeSave callback to run custom logic before the save operation. Set CancelEventArgs.Cancel to true to prevent the save from proceeding.

<MainContext OnBeforeSave="OnBeforeSave">
    <MenuBar>
        <SaveContextButton />
    </MenuBar>

    <RecordContext Record="@_record">
        <TextEdit ColumnName="name" />
    </RecordContext>
</MainContext>

@code {
    private async Task OnBeforeSave(CancelEventArgs eventArgs)
    {
        // Run custom validation or logic
        // Cancel the save if needed:
        // eventArgs.Cancel = true;
    }
}

Example

The following example demonstrates a MainContext with two RecordContexts, a MenuBar with save and refresh buttons, and a custom OnBeforeSave handler.

Blazor example
Save Refresh
Contact Record
Account Record
React example
Blazor

MainContext Class

Parameters

Name
Type
Default
Description
ChildContentRenderFragment?
The child components rendered within this context.
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 this context or any registered child has unsaved changes.
ParentContextMainContext?
The nearest ancestor MainContext, enabling nested context hierarchies.
Name: ChildContent
Type: RenderFragment?
Description: The child components rendered within this context.
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 this context or any registered child has unsaved changes.
Name: ParentContext
Type: MainContext?
Description: The nearest ancestor MainContext, enabling nested context hierarchies.

Events

Name
Type
Description
OnBeforeSaveEventCallback<CancelEventArgs>
Callback called before saving. Allows for cancelling the save operation.
Name: OnBeforeSave
Type: EventCallback<CancelEventArgs>
Description: Callback called before saving. Allows for cancelling the save operation.

Methods

Name
Parameters
Type
Description
GetRequestsList<OrganizationRequest>
Collects and returns all pending save requests from this context and its children.
RefreshAsyncbool forceRefresh
Task
Refreshes this context and all registered child components.
ResetStatevoid
Resets pending changes in this context and all registered children.
SaveAsyncbool? refresh
Task<bool>
Save the context and any child contexts (grid, record, etc).
Validatebool
Validates this context and all registered child components.
Name: GetRequests
Type: List<OrganizationRequest>
Description: Collects and returns all pending save requests from this context and its children.
Name: RefreshAsync
Parameters: bool forceRefresh
Type: Task
Description: Refreshes this context and all registered child components.
Name: ResetState
Type: void
Description: Resets pending changes in this context and all registered children.
Name: SaveAsync
Parameters: bool? refresh
Type: Task<bool>
Description: Save the context and any child contexts (grid, record, etc).
Name: Validate
Type: bool
Description: Validates this context and all registered child components.
React