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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T13:19:42+00:00 2026-06-14T13:19:42+00:00

I have a model binder for a custom type Money . The binder works

  • 0

I have a model binder for a custom type Money. The binder works fine, but the built in binding/validation is kicking in and marking data as invalid.

My binder looks like this:

public class MoneyModelBinder : DefaultModelBinder 
{
    protected override void OnModelUpdated(ControllerContext controllerContext, ModelBindingContext bindingContext)
    {
        var money = (Money)bindingContext.Model;
        var value = bindingContext.ValueProvider.GetValue(bindingContext.ModelName + ".Amount").AttemptedValue;
        var currencyCode = bindingContext.ValueProvider.GetValue(bindingContext.ModelName + ".Iso3LetterCode").AttemptedValue;

        Money parsedValue;
        if (String.IsNullOrEmpty(value))
        {
            money.Amount = null;
            return;
        }

        var currency = Currency.FromIso3LetterCode(currencyCode);

        if(!Money.TryParse(value, currency, out parsedValue))
        {
            bindingContext.ModelState.AddModelError("Amount", string.Format("Unable to parse {0} as money", value));
        }
        else
        {
            money.Amount = parsedValue.Amount;
            money.Currency = parsedValue.Currency;
        }
    }
}

When a user types a value like “45,000” into a textbox my binder correctly picks up the value, parses it and sets it into the model.

The problem I have, is that the default validation then kicks in a states The value '45,000' is not valid for Amount, which as it’s a decimal type makes sense, but I’ve already bound the data. How can I prevent the default binders binding data which I’ve already handled?

I’m not sure if this make any difference, but I’m using Html.EditorFor with and editor that looks like this:

@model Money

<div class="input-prepend">
  <span class="add-on">@Model.Currency.Symbol</span>
    @Html.TextBoxFor(m => m.Amount, new{
                            placeholder=string.Format("{0}", Model.Currency), 
                            @class="input-mini",
                            Value=String.Format("{0:n0}", Model.Amount)
                            })
    @Html.HiddenFor(x => x.Iso3LetterCode)
</div>
  • 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-14T13:19:44+00:00Added an answer on June 14, 2026 at 1:19 pm

    You can just mark the Amount property as read only:

    [ReadOnly(true)]
    public decimal? Amount { get; set; }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is my custom model binder. I have my breakpoint set at BindModel but
I have a custom model binder which pulls an implementation of an interface from
In my application I have a custom model binder that I set to the
So I have one custom model binder that inherits from DefaultModelBinder, where I am
I have a custom model binder inheriting from DefaultModelBinder. What I want it to
In my custom model binder I use bindingContext.ValueProvider.GetValue(propertyName); I do have [ValidateInput(false)] on the
I have in my model a property of a certain type, it's a custom
I have lately read several articles and questions about Model Binding and Validation in
I have created a custom Model Binder that inherits from DefaultModelBinder, and overriding the
I'm using ASP.NET MVC 3 and have my custom model binder. public class NewsModelBinder

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.