c# - ValidationAttribute's value always null -


here's simple validation attribute wrote enforce number-only strings (i use validate phone numbers, postal codes , alike):

public class numericattribute : validationattribute {      public numericattribute() {         errormessage = sharedmodelresources.validationstrings.numeric; // message coming .resx file     }      public override bool isvalid(object value) {          string stringvalue = value string;          if (stringvalue.isnotnull())             foreach (var c in stringvalue)                 if (!char.isdigit(c))                     return false;          return true;     }  } 

the problem when used validate other strings, let's decimal, short, int or else, object value null. why validate numeric decimals? because if don't, default validation message 'the value 'x' not valid 'propertyname'. i'm aware of solutions specific problem of default validation message.

i want understand why it's null...

btw, i'm using vs2008, asp.net mvc 2 (final), .net 3.5 sp1.

are positive value null , not stringvalue?

so sayeth msdn as operator:

the operator cast except yields null on conversion failure instead of raising exception.

http://msdn.microsoft.com/en-us/library/cscsdfbt%28vs.71%29.aspx

in examples on page shows numerics not as strings way want.

you might try .tostring() instead.


Comments

Popular posts from this blog

asp.net - repeatedly call AddImageUrl(url) to assemble pdf document -

java - Android recognize cell phone with keyboard or not? -

iphone - How would you achieve a LED Scrolling effect? -