Method Buttons

Summary

Use MethodButton to run an Order method from a page. Put it inside a DataGraph. Core uses the server's label, permissions, and DisableIf rule.

<MethodButton Method="Post" />

After posting, Post becomes disabled and Undo post becomes available. The server also makes Description and Amount read-only.

Before posting: Post is available.

Draft Order with available Post action

After posting: Undo post is available.

Posted Order with disabled Post action

The complete Order example includes the entity, server logic, graph, and working page. Its C# type is DocsOrder; its UI label is Order.

List the page's methods

Add the page's properties and methods to OrderGraph:

public static GraphBuilder Build() => new GraphBuilder("DocsOrder")
    .Property("Description")
    .Property("Amount")
    .Property("IsPosted")
    .Method("Post")
    .Method("ChangeNote")
    .Method("UndoPost");

Use .Property(...) for data and .Method(...) for methods. Methods can also belong to a PropertyGroup or SplitButton group. Method names stay out of data queries. Existing pages can use buttons without declaring graph methods.

Run a method

<DataGraph Builder="OrderGraph.Build" DataSet="@dataSet" EntityGuid="@guid">
    <PropertyComponent Path="Description" />
    <PropertyComponent Path="Amount" />
    <MethodButton Method="Post" />
    <MethodButton Method="UndoPost" />
    <MethodButton Method="ChangeNote" />
</DataGraph>

The surrounding page creates dataSet and supplies the saved Order's guid. See the complete page for that setup.

Both button types run methods without parameters on the current record. Read uses GET; Modify uses POST. ChangeNote returns a dialog entity and opens a workflow dialog.

Core applies complete record results to the graph. Other successful Modify calls reload the record. Read calls do not reload it.

Reloading and using another dataset

OnInvoked receives the response after the update or reload attempt.

If reloading fails, data stays visible and automatic actions stop. Retry reload reloads data without repeating the method. With an explicit DataSet, the button shows its own warning and retry.

To act on another record of the same entity type, pass its dataset:

<MethodButton Method="Post" DataSet="@selectedOrder" />

The button runs the method on that record and uses its disabled state.

Labels and button state

Setting or state Result
Server label Default text; the method name is the fallback.
Label Overrides the text.
No permission Button is hidden.
Server DisableIf Button stays visible but disabled.
Disabled="true" Page disables the button.
StartIcon, Class, Variant, Color Change appearance.

Buttons show progress while running and reloading. The same dataset cannot run a method twice at once. See the server's DisableIf rule.

Messages

The Order's server handler uses .OnSuccess("Order posted.") after .Do(...). Core shows the message once when posting succeeds. You do not need client code to display it.

Order posted success message

Core shows success, abort, and error messages automatically, even when you set OnInvoked.

Display messages yourself

Set MessageHandling to Manual from Benevia.Core.Client.DataSets:

@using Benevia.Core.Blazor
@using Benevia.Core.Blazor.Components
@using Benevia.Core.Client.DataSets
@using Benevia.Core.Client.OData
@using MudBlazor
@inject ISnackbar Snackbar

<MethodButton Method="Post"
              MessageHandling="MethodMessageHandling.Manual"
              OnInvoked="ShowPostingResultAsync" />

@code {
    private Task ShowPostingResultAsync(CoreODataResponse response)
    {
        Snackbar.ShowServerMessages(response, "Posting failed.");
        return Task.CompletedTask;
    }
}

Show each message once, through Core or your own code. See dataset messages.

Handle the click yourself

Use OnClick for static methods, methods with parameters, or custom dialog behavior. It replaces Core's click handler and does not call OnInvoked. Call dataSet.InvokeMethodAsync to keep Core's reload behavior. Use Body to pass parameters to Modify methods. Read methods do not accept a body.

Choose how to show messages on the dataset call:

var response = await dataSet.InvokeMethodAsync(
    "Post", body: null, messageHandling: MethodMessageHandling.Manual);
Snackbar.ShowServerMessages(response, "Posting failed.");

Custom content

Child content replaces the default button:

<MethodButton Method="Post">
    <MudButton Color="Color.Success" OnClick="HandlePostAsync">Post order</MudButton>
</MethodButton>

Supply HandlePostAsync in the page. Custom content is hidden without permission. You handle its clicks, disabled state, text, and style.

Use a dataset without Blazor

Pass an IModelProvider so the dataset can read the server's method type:

var dataSet = new DataSet(client, graph, modelProvider);
await dataSet.LoadAsync(guid);
var response = await dataSet.InvokeMethodAsync("Post");

DataGraph supplies the server's method details, including for buttons with their own dataset. The two-argument constructor also works. Without method details, the dataset uses POST.

List available methods

Entity metadata lists Name, Label, Type, and IsStatic. When available, Returns describes the result.

var entity = (await modelProvider.GetAllAsync()).First(e => e.Name == "DocsOrder");
var commands = entity.Methods.Where(m => m.Type == MethodType.Modify && !m.IsStatic);

See also