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 6387859
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T03:12:18+00:00 2026-05-25T03:12:18+00:00

My application has many models, many of which contain percentage data. These are represented

  • 0

My application has many models, many of which contain percentage data. These are represented as decimal or decimal? structs in the model. However, not all properties with decimal structs are percentages. Some should be treated like regular decimals.

The percentages need special attention:

  • For display, they should use {0:P2} format. (I have this part working.)
  • For editing, they should allow the same format as the display, i.e. “95” or “95%” or “95.00 %” all bind to a value of 0.95.

I started down the road of creating a PercentModelBinder that implements IModelBinder, but then realized that you can only apply the ModelBinderAttribute to a class, not a property.

What’s the best way to handle this case where some (but not all) uses of a type need special handling both for display and binding?

Every solution I think of smells badly of overkill, fighting the MVC framework. Surely there is a better way than:

  • Creating a custom Percentage struct and using it as the basis for the IModelBinder and EditorTemplates, or
  • Reimplementing the default binding behavior of decimal and decimal? and changing the parsing logic based on intimate knowledge of my model, or
  • Implementing a custom model binder for each class that contains a percentage property, or
  • Using fake proxy properties in the model (i.e. breaking MVC)
  • 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-05-25T03:12:19+00:00Added an answer on May 25, 2026 at 3:12 am

    One possibility is to write a custom metadata aware attribute:

    public class PercentageAttribute : Attribute, IMetadataAware
    {
        public void OnMetadataCreated(ModelMetadata metadata)
        {
            metadata.AdditionalValues["percentage"] = metadata.EditFormatString;
        }
    }
    

    then decorate your view model properties that represent percentages with it:

    public class MyViewModel
    {
        [DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:P2}")]
        [Percentage]
        public decimal? Percentage { get; set; }
    }
    

    and inside the custom model binder test for the presence of this value:

    public class PercentageModelBinder : DefaultModelBinder
    {
        public override object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
        {
            if (bindingContext.ModelMetadata.AdditionalValues.ContainsKey("percentage"))
            {
                var format = (string)bindingContext.ModelMetadata.AdditionalValues["percentage"];
                // TODO: do the custom parsing here
                throw new NotImplementedException();
            }
            else
            {
                // Let the default parsing occur
                return base.BindModel(controllerContext, bindingContext);
            }
        }
    }
    

    Now you can register this model binder to all decimals.

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

Sidebar

Related Questions

i created a small rails application for learning which has 3 models : class
My Core Data model contains an entity, Shape, that has two self-referential relationships, which
I am implementing an application with django, which has a model with a FileField:
Suppose in my rails application I have a model Entry, which has a nested
My rails application has two models, gallery and photo. Galleries have many photos and
I have a rails application in which pages has many comments. So the relationships
Our application has many controls that are created dynamically. For example, a navigation pane
My application has many classes, worker threads, and background service operations. Notifications can come
I have a multithreaded application that has many concurrent operations going on at once.
I'm currently working on a large and old C++ application that has had many

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.