c# - Model Validation from Unit Testing only works for string -


i have got class this

model:

public class circle {     [required(errormessage = "diameter required")]     public int diameter { get; set; }      [required(errormessage = "name required")]     public string color { get; set; } } 

testing:

[testmethod] public void testcircle() {     circle circle = new circle();     circle.diameter = 5;     circle.color = "black";     validationcontext contex = new validationcontext(circle, null, null);     validator.validateobject(circle , contex); } 

i expecting it'd fail whenever diameter or color null. however, above testing failed when string parameter, color, null. why? how should in order validate diameter well?

you shouldn't use required attribute numeric properties. use range attribute instead:

the requiredattribute attribute specifies when field on form validated, field must contain value. validation exception raised if property null, contains empty string (""), or contains white-space characters.


Comments

Popular posts from this blog

javascript - how to protect a flash video from refresh? -

android - Associate same looper with different threads -

visual studio 2010 - Connect to informix database windows form application -