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

  • Home
  • SEARCH
  • 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 8844151
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T11:24:51+00:00 2026-06-14T11:24:51+00:00

This is easier to explain with an example. Suppose I have a person class

  • 0

This is easier to explain with an example. Suppose I have a person class

Public Person
{
string firstName;
string SocialSecurityNumber;
}

When some changes are made by the user in a web page, the Person object is posted back to a controller that accepts Person as the input parameter. The SocialSecurity number is encrypted. We have many pages that post back objects (Not Necessarily Person class) that have encrypted Social security as a parameter. Now I want to modify the model binding, so that if the posted object has SocialSecurityNumber as a property, it should be automatically decrypted. How can I do this?

  • 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-14T11:24:52+00:00Added an answer on June 14, 2026 at 11:24 am

    You could use a custom model binder. Some examples:

    • http://www.dotnetcurry.com/ShowArticle.aspx?ID=584
    • http://odetocode.com/blogs/scott/archive/2009/04/27/6-tips-for-asp-net-mvc-model-binding.aspx
    • http://www.markeverard.com/blog/2011/07/18/creating-a-custom-modelbinder-allowing-validation-of-injected-composite-models/

    This is an example of a simple one I’ve used before that you could modify for your needs:

    public class FormatterModelBinder : DefaultModelBinder
    {
        internal static string TrimValue([CanBeNull] string value, [CanBeNull] FormatAttribute formatter)
        {
            if (string.IsNullOrEmpty(value)) return value;
            return ((formatter == null) || formatter.Trim) ? value.Trim() : value;
        }
    
        protected override void SetProperty(ControllerContext controllerContext, ModelBindingContext bindingContext, PropertyDescriptor propertyDescriptor, object value)
        {
            if ((propertyDescriptor != null) && (propertyDescriptor.PropertyType == typeof(string)))
            {
                var stringValue = value as string;
                var formatter = propertyDescriptor.Attributes.OfType<FormatAttribute>().FirstOrDefault();
                stringValue = TrimValue(stringValue, formatter);
                value = stringValue;
            }
    
            base.SetProperty(controllerContext, bindingContext, propertyDescriptor, value);
        }
    }
    

    You could then create an attribute to decorate the property as required:

    [AttributeUsage(AttributeTargets.Property, AllowMultiple = false)]
    public sealed class FormatAttribute : Attribute
    {
        public FormatAttribute()
        {
            Trim = true;
        }
    
        public bool Trim { get; set; }
    }
    

    This is “activated” by an attribute on the property in the ViewModel

    Public Person
    {
        string firstName;
        [Format(Trim = true)]
        string SocialSecurityNumber;
    }
    

    It should be fairly simple to modify this to allow for encryption.

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

Sidebar

Related Questions

This is easier to explain with an example. Given these two classes: public class
This is easier to explain with some code so I'll give an example first:
Probably an example will be easier to explain this. So imagine i have something
My question is easier to explain with an example: I have a phone number
I think, it is easier explain using an example. Let's take a class that
I guess it's easier to explain using an example: HTML: <div class=boxset> <div class=box_header>
It would be easier to explain with an example. Suppose I wanted to get
It's easier to explain by picture. Sorry for the censoring. In this screen there
I thought that this was easier… I have a asp:hyperlink control, with target=_blank ,
Not sure exactly how to explain this but Imaging you have a table with

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.