I use Visual Studio 2013, Update 5
with the ASP.NET MVC Project Template I created a simple Web to get deeper into MVC after taking a few steps.
I added a Model for which i creaded a standard Edit View and Controller.
In my Model I use "Required" and MinLength Attributes. The MinLength Attibutes raised Validation Messages but if the Fields are kept empty the "Required" Attributes doesn't work.
What works is, if I just put one Character in the field, and leave the field (so that the MinLenth Validation Fails) and afterwards Clear the complete field. Only in this Case, the Required Attribute seems to do anythings. (Bug or Feature?! :-) )
Here is the Model I use with the Edit View completely generated by "new View" Template of Visual Studio
public class Apotheke
{
[DisplayName("Apotheken Nr.")]
[DisplayFormat(DataFormatString = "{0:D4}")]
[Range(1, 9999)]
[Required(AllowEmptyStrings = false)]
public int ApothekenNr { get; set; }
[DisplayName("Name der Apotheke")]
[MinLength(3)]
[Required(AllowEmptyStrings = true)]
public string ApoName { get; set; }
[MinLength(3)]
[Required(AllowEmptyStrings = false)]
public string Straße { get; set; }
[MinLength(5)]
[MaxLength(5)]
[Required(AllowEmptyStrings = false)]
public string PLZ { get; set; }
[MinLength(3)]
[Required(AllowEmptyStrings = false)]
public string Ort { get; set; }
[DisplayName("Inhaber Vorname")]
[MinLength(3)]
[Required(AllowEmptyStrings = false)]
public string Vorname { get; set; }
[DisplayName("Inhaber Nachname")]
[MinLength(3)]
[Required(AllowEmptyStrings = false)]
public string Nachname { get; set; }
[DisplayFormat(DataFormatString = "{0:d}", ApplyFormatInEditMode = true)]
[MinLength(10)]
[MaxLength(10)]
[Required(AllowEmptyStrings = false)]
public DateTime Eintritt { get; set; }
}
[Required]on yourint ApothekenNrandDateTime Eintrittproperties is not necessary (type ofintandDateTimecan't benullso its always required)