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.

Conditional Mandatory

This tutorial explains validating inputs based on a condition. This type of validation should be performed in the model except in certain scenarios.

M# provides "Validate" method, which should be overridden in the entity’s logic class in order to perform conditional validation on a property. Let’s take an example of a simple bool type property "HoldVehicalLicence" on entity Employee, which determines if the employee holds a vehicle license and if answered yes then we ask the type of license.

In above scenario, we cannot mark the "LicenseType" property mandatory, because it depends on the "HoldVehicalLicence". Here, we can perform a conditional validation, which makes "LicenseType" mandatory if the employee holds a licence. The screenshot below shows conditional validation performed in "Validate" method of Employee logic class