For developers: Create new input fields for forms | MVC
You use MVC form fields to collect user input and persist the input in form responses. MVC form fields follow the model-view-controller convention, so you create form fields in a similar workflow as creating widgets in Feather. In addition, similar to standard form fields, you can create the new fields in separate assemblies, following MVC principles of "separation of concerns".
The following tutorial demonstrates how to create a Yes/No field to use in MVC forms. You first create the folder structure, then the custom model, controller, and view. Finally, you register the field in Sitefinity CMS forms toolbox.
Create the custom model
The model represents the data your application works with.
NOTE: This example uses plain C# classes for the model.
To create the custom model:
- In the Models folder, create a new class and name it YesNoFieldModel.
- Open the YesNoFieldModel.cs file and paste the following code:
Create the controller
- In the Controllers folder, create a new class and name it YesNoFieldController.
- Open the YesNoFieldController.cs file and paste the following code:
NOTE: When creating new form fields, your controller can inherit FormFieldControllerBase. Thus, your field is compatible with the forms functionality in Sitefinity CMS. The FormFieldControllerBase provides two actions - Write and Read, which are invoked depending on the Form widget settings. If you want to implement some custom logic in these actions, you can just override them.
Create the view
You implement the view that shows the data from the model, populated in the controller:
- Create the view for the field Write mode:
- Under the class library, in the Mvc/Views/YesNoField folder, create a new code file and name it Write.Default.cshtml.
- In the Properties section of the file, set its Build Action to Embedded Resource.
- Open the file and paste the following code:
- Create the view for the field Read mode:
- Under the class library, in the Mvc/Views/YesNoField folder, create a new code file and name it Read.Default.cshtml.
- In the Properties section of the file, set its Build Action to Embedded Resource.
- Open the file and paste the following code:
Register the field in Sitefinity
- Build the solution.
- In the SitefinityWebApp project, add a reference to the MyFirstFormsField assembly.
As a result, the field is automatically registered in Sitefinity CMS forms toolbox and you can now use the new field on your MVC forms.