M# Tutorials

Learn to build .NET applications with M#. Our step-by-step tutorials will get you up-to-speed rapidly.
If you already know ASP.NET and C#, you can master M# in a week or two.

Input Elements

Creating or updating data is a frequent operation in an application, M# allows you to easily create and implement a page containing a form for populating data.
By using the M# form generation you will be able to sync your model with its form, which means if you delete a property from your model it will also be deleted from your form.
In this lesson we will learn how to generate a form and how it works.

Creation of a Form Module

You learnt in Chapter 6 how to create a module.
In M# a Form is a module, so we will proceed in the same way to create a form for entering information about a new or existing employee. We create a new Form<Employee>.

The following Form module is created:

Generated code

The generated mark-up is a standard ASP.Net code including validators depending on your property attributes specified in the model.

Form-specific attributes

Generate SaveInDatabase()

When you create a form you will need to persist this information, usually when the user clicks on "Save". Within this "Save" button when you create the action "Save in Database" M# will automatically generates the corresponding method.

The problem is that you may have a form without the "Save" button but you still want to be able to save the values. For example you have a page with a list of employees and for each employee a form to edit them, the "Save" button is at the bottom of the page and must save all inner forms. For this scenario you cannot create a button for each form on Employee, instead you will create a unique button which will call the save method of each inner form. For doing this set the "Generate SaveInDatabase()" attribute to "True".

Generate SaveInMemory()

This attribute works like SaveInDatabase(), but instead of persisting data in the database they are saved in the session memory.

Supports add

This attribute allows you to specify if the form allows the user to add a new instance. If set to "False" the form will not be "Create" anymore but "Edit".

M# automatically changes the Employee property getters if you disable the "Add" feature.

Supports edit

This attribute is similar to "Supports add" except it disables the "Edit" feature.

Validation group

The validation group is used to group controls inside a named group and validate all items in this group. By default the name is the name of the type, or the Base type if existing.

Warning styles

The "Warning styles" attribute allows you to define the display when there is a validation exception in the form.

Custom markup

In M# you can specify a custom markup. Let's create a confirmation text box for the Email.

You have to specify the Id of the control, which you will use in your code, the label and the type. If you want to populate this control check at the Custom attributes part. In our example we do not need to populate it or save it, we just need to compare that the value corresponds to the original email address.

Custom attributes

Sometimes for some reasons you may want to overwrite the generated C# code, or when you create a custom control. M# allows you to add flexibility to the generated code by adding the following attributes.

Custom initializer

This attribute allows you to add a piece of C# code in the OnInit() method, for example for loading a list of items.

Custom data load

This attribute allows you to overwrite the C# code for loading the associated control.

Custom data Save

This attribute allows you to overwrite the C# code for saving the associated control.