Search K
Appearance
On this page, we will explain in detail how to build different types of actions.
To build an action, you need to create your own action class that inherits from an abstract ActionBase
class.
There are two abstract methods that need to be overridden:
GetLabel()
Returns the label of the action as shown in the UI. We recommend you use a descriptive label for better readability.Execute(IList<TIdentifier> ids)
Contains the logic of the action to execute. The parameter ids
contains the identifiers of the entities the action should be performed on.A minimal action class might look like this:
using Linkerion.Centias.Pages.Resources.Actions;
public class CustomAction : ActionBase<CustomResource, int>
{
public override GetLabel() => "Update Entity";
public override Task<ActionResult> Execute(IList<int> ids)
{
// Implement your logic here
};
}
Some actions may need parameters to work properly. For example, if you want to increase a numeric value by a set number. In such cases, you can create a parameter class.
public record CustomParams
{
[Required]
public int increaseByValue;
}
The resulting class can then be used as a reference type for TParameters
in ActionBase<TParameters, TResource, TIdentifier>
. A minimal parameterized action may look like this:
using Linkerion.Centias.Pages.Resources.Actions;
public class CustomAction : ActionBase<CustomParams, CustomResource, int>
{
public override GetLabel() => "Update Entity";
public override Task<ActionResult> Execute(IList<TIdentifier> ids,
CustomParams parameters)
{
// Use parameters to change entity according to user input
};
}
An action without any action attribute will be considered a universal action that can be performed on multiple or single entities. The example in Basic Implementation shows a universal action .
With Single Actions users can execute common tasks on a single entity. They are generally useful in scenarios where you want to offer your user a descriptive and straightforward way to interact with an entity.
With the SingleAction
attribute you mark the action to be shown on the entity details view.
using Linkerion.Centias.Pages.Resources.Actions;
[SingleAction]
public class CustomAction : ActionBase<CustomResource, int>
{
public override GetLabel() => "Update Entity";
public override Task<ActionResult> Execute(IList<int> ids)
{
// Implement your logic here
};
}
With Batch Actions, users can execute common tasks on multiple entities that can be selected through the entity list view. With the BatchAction
attribute you mark the action to be applicable to more than one entity.
using Linkerion.Centias.Pages.Resources.Actions;
[BatchAction]
public class CustomAction : ActionBase<CustomResource, int>
{
public override GetLabel() => "Update Entity";
public override Task<ActionResult> Execute(IList<int> ids)
{
// Implement your logic here
};
}
Here we have a complete example for an action to work with our product resource. With this batch action we can update the stocks of one or multiple products by a set amount.
public class UpdateProductStockAction: ActionBase<UpdateProductStockAction.StockUpdateParameters, ProductResource, int>
{
public override string GetLabel() => "Update Product Stock";
public override Task<ActionResult> Execute(IList<int> ids,
StockUpdateParameters parameters)
{
return Task.FromResult(ActionResult.Success());
}
public override IList<IResourceFieldBuilder> Fields() => [
NumberField<int>.Make("Update Product Stock by", "AmountToAdd")
];
public class StockUpdateParameters
{
public int AmountToAdd { get; set; }
}
}
You will see selectors appear on the product index view, as well as an action execute button.