开发者

Force and convert an ASP.NET MVC3 and Entity Framework 4 property to uppercase

开发者 https://www.devze.com 2023-03-10 21:35 出处:网络
I have a database field (postcode) that I want to contain upper case characters, spaces and numbers. No lower case or other punctuation. I\'d like to deal with that as declaratively as possible. One w

I have a database field (postcode) that I want to contain upper case characters, spaces and numbers. No lower case or other punctuation. I'd like to deal with that as declaratively as possible. One way would be to use a regular expression DataAnnotation to validate, [A-Z][0-9]\w (or similar - I'm not a regular expression expert, but I'm sure the right regular expression is there to be found). That would do half of what I开发者_StackOverflow社区 want - it would force the user to input in uppercase, but it wouldn't convert to uppercase.

If I want to convert to uppercase there are some solutions in Stack Overflow question How can I force input to uppercase in an ASP.NET textbox?, but none of them are particularly declarative. At the moment I'm thinking of a combination of the validation and jQuery to spot anything with a class of "uppercaseonly" and convert the data, but it's a bit messy.

Am I missing anything?


CSS text-transform: uppercase?

http://www.w3schools.com/css/pr_text_text-transform.asp

UPDATE:

Try implementing a custom ModelBinder. Here's an example: http://www.agileatwork.com/custom-model-binder-in-asp-net-mvc/

Instead of AddRoles just set the property to uppercase. No need to touch your EF classes.


Why not just change the setter to store the received value as uppercase?

So, in your class object, instead of the shorthand property { get; set; } change to;

get { return this.property; }

set { this.property = value.ToUpper(); }
0

精彩评论

暂无评论...
验证码 换一张
取 消