0

Is it possible to use an attribute to modify a model value?

eg.

[TrimInput]
public string SomeName {get;set;}

public string TrimInputAttribute : Attribute
{
    /// do something here?
}
Christoph Fink
  • 22,727
  • 9
  • 68
  • 113
monkeyhouse
  • 2,875
  • 3
  • 27
  • 42
  • 1
    possible duplicate of [How to get and modify a property value through a custom Attribute?](http://stackoverflow.com/questions/2864343/how-to-get-and-modify-a-property-value-through-a-custom-attribute) – Satpal Jun 16 '14 at 14:11
  • Yep. Is duplicate. I thought he was trying to do something else cause the implementation of the validation attribute seemed odd. – monkeyhouse Jun 16 '14 at 15:56
  • 1
    I also found this usefull* - override the DefaultModel binder for these scenarios *http://stackoverflow.com/questions/1718501/asp-net-mvc-best-way-to-trim-strings-after-data-entry-should-i-create-a-custo – monkeyhouse Jun 16 '14 at 16:45

1 Answers1

1

Yes, it is - but you need something to EXECUTE it. Attributes never execute any code.

What you can do is having a "ModelFixer" class that has a method that you call - and that is evaluationg the attributes and doing the operations.

All other attributes work like that - there is always a component USING the attribute (which sometimes is the runtime or the compiler).

TomTom
  • 61,059
  • 10
  • 88
  • 148