Skip to main content
A newer version of this page is available. .
All docs
V22.1

Add a Simple Action (.NET 6)

  • 4 minutes to read

This lesson explains how to create a Simple Action.

A Simple Action is a button that executes custom code when a user clicks it.

The instructions below demonstrate how to add the Clear tasks button to the Contact Detail View. A click on this button clears all Tracked Tasks of the specific Contact.

Blazor simple action button

Note

Before you proceed, take a moment to review the previous lessons:

  • Inherit from the Business Class Library Class (XPO/EF core)
  • Implement Custom Business Classes and Reference Properties (XPO/EF core)
  • Set a Many-to-Many Relationship (XPO/EF core)

Step-by-Step Instructions

  1. Add a View Controller. In the Solution Explorer, right-click the Controllers folder in the MySolution.Module project and choose Add DevExpress Item | New Item… to invoke the Template Gallery. Select the XAF Controllers | View Controller Visual Studio template, specify ClearContactTasksController as the new item’s name and click Add Item.

    Tutorial_EF_Lesson1_1

  2. Visual Studio displays an autogenerated ClearContactTasksController.cs file with a single View Controller declaration. In the controller constructor, specify the controller properties:

    using DevExpress.ExpressApp;
    
    //...
    public partial class ClearContactTasksController : ViewController {
        public ClearContactTasksController() {
            InitializeComponent();
            //Activate the Controller only in the Detail View
            TargetViewType = ViewType.DetailView;
            //Specify the type of objects that can use the Controller
            TargetObjectType = typeof(Contact);
        }
    // ...
    

    If you do not specify the TargetObjectType property, the application displays the controller’s actions for each detail form.

    Tip

    You can also create a generic ViewController<ViewType> or ObjectViewController<ViewType, ObjectType> object and specify the target type as the ViewType parameter. For more information on how to customize controller functionality, refer to the following topic: Define the Scope of Controllers and Actions.

  3. Add a new action to the controller and a handler for the action’s Execute event:

    using DevExpress.ExpressApp;
    using DevExpress.ExpressApp.Actions;
    using DevExpress.Persistent.Base;
    using MySolution.Module.BusinessObjects;
     //...
    public partial class ClearContactTasksController : ViewController {
        public ClearContactTasksController() {
            InitializeComponent();
            TargetViewType = ViewType.DetailView;
            TargetObjectType = typeof(Contact);
    
            SimpleAction clearTasksAction = new SimpleAction(this, "ClearTaskAction", PredefinedCategory.View) {
                //Specify the Action's button caption.
                Caption = "Clear tasks",
                //Specify the confirmation message that pops up when a user executes an Action.
                ConfirmationMessage = "Are you sure you want to clear the Tasks list?",
                //Specify the icon of the Action's button in the interface.
                ImageName = "Action_Clear"
            };
            //This event fires when a user clicks the Simple Action control.
            clearTasksAction.Execute += ClearTasksAction_Execute;
        }
        private void ClearTasksAction_Execute(Object sender, SimpleActionExecuteEventArgs e) {
            while(((Contact)View.CurrentObject).Tasks.Count > 0) {
                ((Contact)View.CurrentObject).Tasks.Remove(((Contact)View.CurrentObject).Tasks[0]);
                ObjectSpace.SetModified(View.CurrentObject);
            }
        }
    // ...
    

    You can use one of the standard images as the icon of the Action’s button or import your own.

    The main entry point of a Simple Action is its Execute event. Handle this event to execute custom code.

  4. Run the application.

    Open a detail form for a Contact item. Link several tasks to this item and save it.

    Click the Clear tasks button. A confirmation message appears. Click OK to remove all Tasks from the current Contact.

    XAF ASP.NET Core Blazor simple action remove assigned objects

    Tip

    You can display an action’s button in a Detail View layout instead of a toolbar: How to: Include an Action in a Detail View Layout.

Next Lesson

Add a Parameterized Action

See Also