Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8212835
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T10:54:17+00:00 2026-06-07T10:54:17+00:00

I am working on an ASP.NET MVC 2 application that I didn’t create. All

  • 0

I am working on an ASP.NET MVC 2 application that I didn’t create. All input fields in the application are trimmed during model binding. However, I want to have a NoTrim attribute that prevents certain fields from being trimmed.

For instance, I have the following state drop-down field:

<select name="State">
    <option value="">Select one...</option>
    <option value="  ">International</option>
    <option value="AA">Armed Forces Central/SA</option>
    <option value="AE">Armed Forces Europe</option>
    <option value="AK">Alaska</option>
    <option value="AL">Alabama</option>
    ...

The problem is that when a user chooses “International”, I get a validation error because the two spaces are trimmed and State is a required field.

Here’s what I’d like to be able to do:

    [Required( ErrorMessage = "State is required" )]
    [NoTrim]
    public string State { get; set; }

Here’s what I’ve got for the attribute so far:

[AttributeUsage( AttributeTargets.Property, AllowMultiple = false )]
public sealed class NoTrimAttribute : Attribute
{
}

There is a custom model binder that gets set up in Application_Start:

protected void Application_Start()
{
    ModelBinders.Binders.DefaultBinder = new MyModelBinder();
    ...

Here is the part of the model binder that does the trimming:

protected override void SetProperty( ControllerContext controllerContext,
                                     ModelBindingContext bindingContext,
                                     PropertyDescriptor propertyDescriptor,
                                     object value )
{
    if (propertyDescriptor.PropertyType == typeof( String ) && !propertyDescriptor.Attributes.OfType<NoTrimAttribute>().Any() )
    {
        var stringValue = (string)value;

        if (!string.IsNullOrEmpty( stringValue ))
        {
            value = stringValue.Trim();
        }
    }

    base.SetProperty( controllerContext, bindingContext, propertyDescriptor, value );
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T10:54:19+00:00Added an answer on June 7, 2026 at 10:54 am

    NoTrim looks good but it’s that [Required] attribute that will reject whitespace.

    The RequiredAttribute attribute specifies that when a field on a form
    is validated, the field must contain a value. A validation exception
    is raised if the property is null, contains an empty string (“”), or
    contains only white-space characters.

    http://msdn.microsoft.com/en-us/library/system.componentmodel.dataannotations.requiredattribute.aspx

    To workaround the issue, you could create your own version of the attribute or use a RegexAttribute. I’m not sure the AllowEmptyStrings property would work.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm working on an ASP.NET MVC web application that will be deployed across multiple
I am working in an ASP.NET MVC Application. I have a view model as
I have an ASP.NET MVC application that I'm working on. I've been developing it
I am working with an ASP.NET MVC application. There is a requirement that a
I'm working on an asp.net MVC application. I have a class that wraps a
I am working on an ASP.NET MVC application that contains a header and menu
I am working on maintaining a ASP.NET MVC application that has the following coding
Background I'm working on building an ASP.Net MVC 3 application that utilizes: JQueryUI for
I have an ASP.NET MVC (beta) application that I'm working on, and am having
I'm looking for a working sample of an ASP.NET MVC web application that uses

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.