0

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; }

  }
7
  • have you tried this [Required] ? Commented Aug 25, 2015 at 7:45
  • Without using AllowEmptyStrings=false ,this should work [Required] Commented Aug 25, 2015 at 7:46
  • 2
    Not related, but [Required] on your int ApothekenNrand DateTime Eintritt properties is not necessary (type of int and DateTime can't be null so its always required) Commented Aug 25, 2015 at 7:47
  • yes, I tried [Required] (so without AllowEmptyStrings=false) and also tried AllowEmptyStrings=true out of desperation Commented Aug 25, 2015 at 7:58
  • 1
    plz show your view code and controller code , may be something is missing there Commented Aug 25, 2015 at 8:03

1 Answer 1

2

AllowEmptyStrings = false raises a validation error when user enters blank spaces. It is working fine in below framework. Please try validating object in controller to make sure your annotations are correct. Check ModelState errors.

To test your issue I am using below packages on .NET 4.5 (VS 2013, MVC 5):

  • "EntityFramework" version="6.1.3"
  • "jQuery" version="2.1.4"
  • "jQuery.Validation" version="1.11.1"

    public ActionResult Edit(string id)
    {
        ...
        returningModel.PLZ = "  ";
        //returningModel.PLZ = null;
    
        bool b = TryValidateModel(returningModel); 
        var modelStateErrors = ModelState.Values.SelectMany(m => m.Errors);
        return View(returningModel);
    }
    
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.