Gcobani Mkontwana

Gcobani Mkontwana

  • 1.2k
  • 765
  • 36.4k

CustomValidation not working on asp.net mvc form?

Jun 29 2020 3:06 PM
Hi Team
 
I  need some help, my customValidation does not work and when i debug its showing any error. Please assist mates, basically the logic i want when leaving the Razor@Htm.EditorFor(). Must validate message error "This field is required".
 
  1. // Model class  
  2. [Required(ErrorMessage = "This field is required")]  
  3.           
  4.         [DataType(DataType.PhoneNumber)]  
  5.           
  6.         public string CellNumber { getset; }  
  7.   
  8.   
  9. // CustomValidation.cs  
  10. using System;  
  11. using System.Collections.Generic;  
  12. using System.Linq;  
  13. using System.Web;  
  14. using eNtsaRegistrationTraining.Models;  
  15. using System.ComponentModel.DataAnnotations;  
  16.   
  17. namespace eNtsaRegistrationTraining.CustomValidation  
  18. {  
  19.     public class CustomFormValidation:ValidationAttribute  
  20.     {  
  21.         protected override ValidationResult IsValid(object value, ValidationContext validationContext)  
  22.         {  
  23.             var registration = (TrainingRegForm)validationContext.ObjectInstance;  
  24.             if(registration.CellNumber==null)  
  25.             {  
  26.                 return new ValidationResult("This field is required");  
  27.             }  
  28.                   
  29.             return base.IsValid(value, validationContext);  
  30.         }  
  31.     }  
  32. }  
  33.   
  34. // Controller.  
  35.     //GET:TrainingRegForm/Create/WebRequest.  
  36.         [HttpPost]  
  37.         [ValidateAntiForgeryToken]  
  38.         public ActionResult SubmitRegDetails([Bind(Include= "Id, Title, FirstName, LastName, Position, Company, StreetAddress, StreetAddressLine, City, StateProvince, ZipCode,Email, CellNumber, DietaryRequirement")]TrainingRegForm eNtsaTraining)  
  39.         {  
  40.             if(ModelState.IsValid)  
  41.             {  
  42.                 eNtsaTraining.Id = Guid.NewGuid();  
  43.                 db.TrainingRegs.Add(eNtsaTraining);  
  44.                 db.SaveChanges();  
  45.                 return RedirectToAction("SaveRegForm");  
  46.             }  
  47.   
  48.             // Validates when empty.  
  49.             if(ModelState.IsValid)  
  50.             {  
  51.                 return RedirectToAction("SaveRegForm");  
  52.             }  
  53.             return View(eNtsaTraining);  
  54.         }  
  55.   
  56. // View  
  57. div class="form-group row">  
  58.                             <label for="Attendee" class="col-sm-2 col-form-label">Attendee Cell Number*</label>  
  59.                             <div class="col-sm-3">  
  60.                                 @Html.EditorFor(model => model.RegForm.CellNumber, new { htmlAttributes = new { @class = "form-control", autofocus = "autofocus", placeholder = "Cell Number" } })  
  61.                                 @Html.ValidationMessageFor(model => model.RegForm.CellNumber, ""new { @class = "text-danger" })  
  62.                             </div>  
  63.                         </div>  
 

Brought to you by: JavaScript SDK for Bold BI dashboard and analytics embedding. Free trial.

Answers (2)