2

I need to do trim to all HTTP POST data submitted by users through web forms. Having done googling, apparently there is no in-built functionality in asp.net to trim all HTTP POST data.

The closest that I can get is what is described here: ASP.NET MVC: Best way to trim strings after data entry. Should I create a custom model binder?

Unfortunately it doesn't work on nested ViewModels (ViewModel with property with type of other ViewModel).

What is the best way to achieve this? I don't want to do property.Trim() on every properties in all ViewModel. Thank you.

Community
  • 1
  • 1
user895852
  • 33
  • 5
  • Custom model binder is the way to go. Why does it not work with nested view models? You should be able to implement that. – Yuck Aug 16 '11 at 02:24
  • Looking at the code again I just found out that some of the values are obtained directly from Request.Form ... that's why it doesn't work for some properties. I would consider this question as answered. – user895852 Aug 16 '11 at 02:56

1 Answers1

1

One option is to define your own IValueProvider. I would start by inheriting from NameValueCollectionValueProvider to make a TrimmedNameValueCollectionValueProvider in which you trim the results as you pull them out. Then you would defined a TrimmedFormValueProvider that passes in controllerContext.HttpContext.Request.Form as the collection.

dahlbyk
  • 75,175
  • 8
  • 100
  • 122