I have got an extension method which I call from within my EditorTemplate (for a string) which sets the Label and the Required metadata on the on the control. (I cannot annotate my models so this is why I am doing this).
public static void SetMetadata(this ViewUserControl control)
{
if (!string.IsNullOrEmpty((string)control.ViewData["labeltext"]))
control.ViewData.ModelMetadata.DisplayName = (string)control.ViewData["labeltext"];
if (control.ViewData["required"] != null)
control.ViewData.ModelMetadata.IsRequired = (bool)control.ViewData["required"];
}
Has anyone got any ideas why ModelState.IsValid would not be picking up on the fields I have set to Required = true?