0

When a user fill out my form to create a new Person, I'd like there to be no whitespace before or after a name (type String).

Good: "John Doe"

Bad: "John Doe " or " John Doe"

Looking at this SO post, it seems that I want to use a custom ModelBinder. However, as I perhaps incorrectly understand the post, replacing my DefaultModelBinder will mean that all strings will not be allowed to have leading or trailing whitespace.

How can I ensure that only Name's are affected by this custom ModelBinder?

Community
  • 1
  • 1
Kevin Meredith
  • 41,036
  • 63
  • 209
  • 384
  • The point of that linked SO question was to not explicitly Trim() every string so I guess thats the case here too. I wonder if using such a sophisticated way for only one field is a good idea though. – S_F Jul 19 '13 at 15:39
  • A model binding solution sounds like overkill to me. – Adrian Thompson Phillips Jul 19 '13 at 15:56
  • @S_F, since the other post is searching by type String, how isn't every string getting trimmed? Hi Adrian - thanks for pointing out the overkill. – Kevin Meredith Jul 19 '13 at 16:16

3 Answers3

2

You could write this behaviour straight into your view-model (if you're using view-models):

private string name;

public string Name
{
    get { return this.name; }

    set { this.name = value.Trim(); }
}

Then the Name will arrive pre-trimmed in your controller action method.

Adrian Thompson Phillips
  • 6,893
  • 6
  • 38
  • 69
  • I'm not sure what you mean by Boe Title (typo?), however it may be better to add a similar bit of code into your domain/application entity instead (or as well as). I'd avoid adding any logic like this into your DB queries or stored procedures though. – Adrian Thompson Phillips Jul 19 '13 at 16:05
0

You can use Trim function. From MSDN,

The Trim method removes from the current string all leading and trailing white-space characters.

young
  • 2,163
  • 12
  • 19
  • Yeah, thanks. I'm still figuring out the right place to put the `Trim()` – Kevin Meredith Jul 19 '13 at 15:46
  • @Kevin Doesn't your form(dialog?) have a 'Ok" button for user to submit what they fill? I think you just need to post-process their name when they actually press 'ok' or whatever button that makes close your form. – young Jul 19 '13 at 15:51
0

You can have have name mentioned in property for ex:

    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string FullName { get; set; }

And then you can use the modified code below for the Custom Model binder solution in the post you have mentioned:

public class TrimModelBinder : DefaultModelBinder
{
    protected override void SetProperty(ControllerContext controllerContext,
                                        ModelBindingContext bindingContext,
                                        System.ComponentModel.PropertyDescriptor propertyDescriptor, object value)
    {
        if (propertyDescriptor.Name.ToUpper().Contains("NAME")
             && (propertyDescriptor.PropertyType == typeof(string)))
        {
            var stringValue = (string) value;
            if (!string.IsNullOrEmpty(stringValue))
                stringValue = stringValue.Trim();

            value = stringValue;
        }

        base.SetProperty(controllerContext, bindingContext,
                         propertyDescriptor, value);
    }
}

This way any name property that name in it and is type of string will have the white spaces trimmed as you want here.

Yogiraj
  • 1,952
  • 17
  • 29