1. RequiredIfAttribute.cs using Microsoft.AspNetCore.Mvc.ModelBinding.Validation; using System; using System.Collections.Generic; using System.ComponentModel.DataAnnotations; namespace Core3Razor.ModelValidation { public class RequiredIfAttribute : ValidationAttribute, IClientModelValidator { private readonly string _comparisonProperty; public RequiredIfAttribute(string comparisonProperty) { _comparisonProperty = comparisonProperty; } protected override ValidationResult IsValid(object value, ValidationContext validationContext) { ErrorMessage = ErrorMessageString; //get entered WorkPass Type var workpasstype = value == null ? "" : value.ToString(); var property = validationContext.ObjectType.GetProperty(_comparisonProperty); if (property == null) throw new ArgumentException("Property with this name not found"); //get the workpasstype value var workpasstypeValue = (string)property.GetValue(validationContext.ObjectInstance); if (workpasstype == "" && workpasstypeValue != null) return new ValidationResult(ErrorMessage); return ValidationResult.Success; } public void AddValidation(ClientModelValidationContext context) { MergeAttribute(context.Attributes, "data-val", "true"); MergeAttribute(context.Attributes, "data-val-requiredif", "Since select the WorkPass type, please select WorkPass Expiry Date"); } private static bool MergeAttribute(IDictionary attributes, string key, string value) { if (attributes.ContainsKey(key)) { return false; } attributes.Add(key, value); return true; } } } 2. BiodataViewModel.cs using Core3Razor.ModelValidation; using System; using System.ComponentModel.DataAnnotations; namespace Core3Razor.Data { public class BiodataViewModel { //--WorkpassType ---// [Required(ErrorMessage = "Please Select WorkPass Type")] [Display(Name = "WorkPass Type"), StringLength(20)] public string WorkpassType { get; set; } //--Workpass Expiry Date ---// //[Required(ErrorMessage = "Please Select WorkPass Expiry Date")] [RequiredIf("WorkpassType", ErrorMessage = "Please select WorkPass Expiry Date")] [Display(Name = "WorkPass Expiry Date")] [DisplayFormat(DataFormatString = "{0:dd/MM/yyyy}", ApplyFormatInEditMode = true)] [DataType(DataType.Date)] public DateTime? WorkpassExpiryDate { get; set; } } } 3. _BioData.cshtml @model Core3Razor.Data.BiodataViewModel
4. CreateBioData.cshtml @page @model Core3Razor.Pages.CreateBioDataModel
@section Scripts{ @{await Html.RenderPartialAsync("_ValidationScriptsPartial");} } 5. CreateBioData.cshtml.cs using Core3Razor.Data; using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc.RazorPages; using System; namespace Core3Razor.Pages { public class CreateBioDataModel : PageModel { [BindProperty] public BiodataViewModel Biodata { get; set; } public void OnGet() { Biodata = new BiodataViewModel(); } public IActionResult OnPost() { //server side validation if (!ModelState.IsValid) { return Page(); } var WType = Biodata.WorkpassType; var WDate = Biodata.WorkpassExpiryDate; return RedirectToPage(nameof(Index)); } public void OnPostInsertBioData() { var newitem = Biodata; } } }