c# - ASP.Net MVC Web does not validate empty fields -
i use visual studio 2013, update 5
with asp.net mvc project template created simple web deeper mvc after taking few steps.
i added model creaded standard edit view , controller.
in model use "required" , minlength attributes. minlength attibutes raised validation messages if fields kept empty "required" attributes doesn't work.
what works is, if put 1 character in field, , leave field (so minlenth validation fails) , afterwards clear complete field. in case, required attribute seems anythings. (bug or feature?! :-) )
here model use edit view generated "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; } }
allowemptystrings = false
raises validation error when user enters blank spaces. working fine in below framework. please try validating object in controller make sure annotations correct. check modelstate
errors.
to test issue 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); }
Comments
Post a Comment