Note that there are some explanatory texts on larger screens.

plurals
  1. POMVC Custom Validation Attribute being overwritten by Default ModelValidater..The Value "null" is not valid
    text
    copied!<p>I have a model with some bool objects </p> <pre><code> [DisplayName("Is student still at school")] //[ValidBoolDropDown("IsStillAtSchool")] public bool? IsStillAtSchool { get; set; } </code></pre> <p>that are being implemented with some bool editor dropdown templates </p> <pre><code> @model bool? @{ int intTabIndex = 1; if (ViewData["tabindex"] != null) { intTabIndex = Convert.ToInt32(ViewData["tabindex"]); } } @{ string strOnChange = ""; if (ViewData["onchange"] != null) { strOnChange = ViewData["onchange"].ToString(); } } &lt;div class="editor-field"&gt; @Html.LabelFor(model =&gt; model): @Html.DropDownListFor(model =&gt; model, new SelectListItem[] { new SelectListItem() { Text = "Yes", Value = "true", Selected = Model == true ? true : false }, new SelectListItem() { Text = "No", Value = "false", Selected = Model == false ? true : false }, new SelectListItem() { Text = "Select", Value = "null", Selected = Model == null ? true : false} }, new { @tabindex = intTabIndex, @onchange = strOnChange }) @Html.ValidationMessageFor(model =&gt; model) &lt;/div&gt; </code></pre> <p>On the post I still get the default model validation error </p> <p>The value 'null' is not valid for Is studentstill at school.(aka IsStillatSchool)</p> <p>I have even implemented a custom ValidationAttribute </p> <pre><code>public class ValidBoolDropDown : ValidationAttribute { public ValidBoolDropDown(string dropdownname) :base("Please Select for {0}") { DropDownName = dropdownname; } private string DropDownName; protected override ValidationResult IsValid(object value,ValidationContext validationContext) { var boolres = GetBool(validationContext); //if (!boolres.HasValue) //{ // return new ValidationResult(FormatErrorMessage(validationContext.DisplayName)); //} return ValidationResult.Success; } public override string FormatErrorMessage(string name) { return string.Format(ErrorMessageString, name); } protected bool? GetBool(ValidationContext validationContext) { var propertyInfo = validationContext .ObjectType .GetProperty(DropDownName); if (propertyInfo != null) { var boolValue = propertyInfo.GetValue(validationContext.ObjectInstance, null); if (boolValue == null) return null; return boolValue as bool?; } return null; } } </code></pre> <p>This fires but gets overwritten and the Model.Value.Error for this attribute still fails</p> <p>I saw some about the turning the automatic required flag off for value types in Glocal.asx</p> <pre><code> DataAnnotationsModelValidatorProvider.AddImplicitRequiredAttributeForValueTypes = false; </code></pre> <p>but this has not worked..is it a case to create a custom MetadataValidatorProvider for the app or is there something else going on </p> <p>Thanks in Adavance</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload