1

I have a model binder in Global.asax:

ModelBinders.Binders.Add(typeof(string), new StringModelBinder());

And I have a model in which I want to ignore the binder for one of string properties:

public class MyModel
{
    ...
    public string StringProp { get; set; }
    ...
}

For instance StringModelBinder trims my string. But I don't want to trim StringProp. How can I ignore the binder in such case?

EDITED: I am not looking for a solution to trim a string. I'm looking for a solution to ignore trimming for some string.

T.S.
  • 18,195
  • 11
  • 58
  • 78
Neshta
  • 2,605
  • 2
  • 27
  • 45
  • possible duplicate of [ASP.NET MVC: Best way to trim strings after data entry. Should I create a custom model binder?](http://stackoverflow.com/questions/1718501/asp-net-mvc-best-way-to-trim-strings-after-data-entry-should-i-create-a-custo) – Brad C Jun 25 '15 at 13:13
  • 3
    Maybe create a custom attribute, put it on this property, and change your custom model binder to ignore properties with this attribute. You may find interesting infos here (even if that's the opposite) : http://stackoverflow.com/questions/2186969/custom-model-binder-for-a-property – Raphaël Althaus Jun 25 '15 at 13:13
  • @RaphaëlAlthaus, yes, I thought about a custom attribute. But I'm looking for a better solution. May be something out of the box. – Neshta Jun 25 '15 at 13:22
  • My bet would be also with @RaphaëlAlthaus since you want for "some" strings and not others. It's a simple solution to implement and conceptually simple to understand. Even DontTrimAttribute would be fine and concize. – Martin Gemme Jun 25 '15 at 13:30
  • http://stackoverflow.com/questions/8332594/how-do-you-exclude-properties-from-binding-when-calling-updatemodel – JsonStatham Jun 25 '15 at 13:31
  • @SelectDistinct, it ignores binding at all. I want to ignore only custom model binder. – Neshta Jun 25 '15 at 14:04

0 Answers0