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

Add a Parametrized Action (.NET 6)

  • 4 minutes to read

This lesson explains how to add a Parametrized Action. The Parametrized Action displays an editor that allows users to type in a parameter value before running the action.

The instructions below demonstrate how to implement a new View Controller with a Parametrized Action. This Action searches for a DemoTask object by its Subject property value and displays the detail form of the found object.

Note

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

Step-by-Step Instructions

  1. Add a new View Controller to the MySolution.Module project, as described in the Add a Simple Action lesson. Name it FindBySubjectController.

  2. In the MySolution.Module | Controllers | FindBySubjectController.cs file, specify the controller’s properties:

    using DevExpress.ExpressApp;
    // ...
    public partial class FindBySubjectController : ViewController {
        public FindBySubjectController() {
            InitializeComponent();
            //Activate the controller only in the List View.
            TargetViewType = ViewType.ListView;
            //Activate the controller only for root Views.
            TargetViewNesting = Nesting.Root;
            //Specify the type of objects that can use the controller.
            TargetObjectType = typeof(DemoTask);
        }
        // ...
    }
    

    For more information about the root View, see the following topic: IsRoot.

  3. Add a Parametrized Action to the Controller:

    public partial class FindBySubjectController : ViewController {
        public FindBySubjectController() {
            InitializeComponent();
            TargetViewType = ViewType.ListView;
            TargetViewNesting = Nesting.Root;
            TargetObjectType = typeof(DemoTask);
    
            ParametrizedAction findBySubjectAction =
                new ParametrizedAction(this, "FindBySubjectAction", PredefinedCategory.View, typeof(string)) {
                    ImageName= "Action_Search",
                    NullValuePrompt = "Find task by subject..."
                };
            findBySubjectAction.Execute += FindBySubjectAction_Execute;
        }
    // ...
    }
    

    A user submits a string in the Action’s editor. This raises the Action’s ParametrizedAction.Execute event.

  4. Handle the Action’s Execute event:

    public partial class FindBySubjectController : ViewController {
        public FindBySubjectController() {
         // ...
            findBySubjectAction.Execute += FindBySubjectAction_Execute;
        }
        private void FindBySubjectAction_Execute(object sender, ParametrizedActionExecuteEventArgs e) {
            var objectType = ((ListView)View).ObjectTypeInfo.Type;
            IObjectSpace objectSpace = Application.CreateObjectSpace(objectType);
            string paramValue = e.ParameterCurrentValue as string;
            object obj = objectSpace.FindObject(objectType, 
                CriteriaOperator.Parse("Contains([Subject], ?)",  paramValue));
            if(obj != null) {
                DetailView detailView = Application.CreateDetailView(objectSpace, obj);
                detailView.ViewEditMode = DevExpress.ExpressApp.Editors.ViewEditMode.Edit;
                e.ShowViewParameters.CreatedView = detailView;
            }
        }
    // ...
    }
    

    For details on the event handler implementation, refer to the Detailed Explanation section.

  5. Run the application.

    Select the Task item in the navigation control (the Find Task by Subject editor is the Action you implemented). Type a word from an existing task’s Subject into this editor and press Enter. The application displays a detail form with this task.

    blazor tutorial parameterized action

Detailed Explanation

Search Implementation

In XAF, you use the Object Space to query and update persistent objects. Call the static XafApplication.CreateObjectSpace method to create an Object Space.

Use the IObjectSpace.FindObject method to find a DemoTask object. This method has the following parameters:

  • The type of the objects displayed in the current List View. Use the View.ObjectTypeInfo.
  • Search criteria. To generate a criteria, create a BinaryOperator object and pass criteria components as the constructor’s parameters. For more information, refer to the following XPO documentation: Simplified Criteria Syntax.

Create a New View

To show the found object in a separate Detail View:

  1. Call the XafApplication.CreateDetailView method to create a View.
  2. Assign the View to the e.ShowViewParameters.CreatedView property of the event parameter.

Tip

You can initialize the ShowViewParameters property in the Execute event handler of any Action of any type. This allows you to always show a View after an Action is executed.

For more information on how to show a View in a separate window, refer to the following topic: Ways to Show a View.

Manage an XAF Application

The XafApplication object is useful when you need to create a List View, Detail View, Object Space, etc. A user can access an XafApplication object from many locations in an XAF application. In Controllers, use the Controller.Application to get such access.

Next Lesson

Add an Action that Displays a Pop-up Window

See Also