I managed to get this job, and this is my decision for others to appreciate.
As soon as we note that a complete solution cannot be reached because I could not get the Modelmetadata inside the StringValidation.IsValid() attribute. The special question I had here was that I could get metadata, however I could not get PropertyName from it, only DisplayName . There were several options, but the fact that some of my properties have the same DisplayName means that I could not be sure that ProprtyName was the one I was actually checking.
Here is the code for ValidationAttribute :
public class StringValidationAttribute : ValidationAttribute, IClientValidatable, IMetadataAware { private bool _uppercase; public StringValidationAttribute(bool uppercase = false) { _uppercase = uppercase; } ... public void OnMetadataCreated(ModelMetadata metadata) { metadata.AdditionalValues["Uppercase"] = _uppercase; } }
Then I created a new implementation of IModelBinder :
public class StringBinder : IModelBinder { public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext) { ValueProviderResult result = bindingContext.ValueProvider.GetValue(bindingContext.ModelName); if (result == null) return null; if (bindingContext.ModelMetadata.AdditionalValues.ContainsKey("Uppercase")) { if ((bool)bindingContext.ModelMetadata.AdditionalValues["Uppercase"]]) return result.AttemptedValue.ToUpper(); } return result.AttemptedValue; } }
And registered that in my Global.asax file:
ModelBinders.Binders.Add(typeof(string), new StringBinder());
The code still leads to the fact that any line input coming into MVC will be converted to upper case if StringValidationAttribute attached to it and where the upper indicator is set.
Further, in order to achieve my desire to make html forms also in capital letters, I implemented a new EditorTemplate called string.cshtml . In this view, I added:
RouteValueDictionary htmlAttributes = new RouteValueDictionary(); if ((bool)ViewData.ModelMetadata.AdditionalValues["Uppercase"]) { htmlAttributes.Add("class", "Uppercase"); } @Html.TextBox("", Model, htmlAttributes)
With CSS as;
.Uppercase { text-transform: uppercase; }
Hope this post helps some others out there.