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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T10:34:27+00:00 2026-06-13T10:34:27+00:00

I’m using ASP.NET MVC 3 for a project I have. The problem is that

  • 0

I’m using ASP.NET MVC 3 for a project I have.

The problem is that I would like to save a value to the database, which should before saving into the database be “cleaned” (say removing trailing and ending spaces, and also validate).

I use a MetaData Class to validate the models before I save it to the database with data annotations, using the following code to validate:

if (ModelState.IsValid) {

My MetaData Class looks like this:

public class OrganizationMD {
    [Required(ErrorMessage = "*This field is required.")]
    [CustomValidationRule(ErrorMessage = "*Another error message")]
    public string OrganizationNumber;
}

My first idea was to give the OrganizationNumber string a getter and setter, and there let the value become “fixed”.

For example if someone tries to save a company with “19860415-4785” as organizationnumber, it should automatically remove the trailing “19” and the dash in the string before validating and saving that new value into the database.

I can’t give the model a getter and setter because we’re developing using Model-First, otherwise I think that should work.

Does anyone have any idea how to solve 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-13T10:34:28+00:00Added an answer on June 13, 2026 at 10:34 am

    You could create a view model to pass the the data to/from the view and controller instead of using your domain (entity framework) model directly.

    Put your data annotations on the view model’s property.

    public class OrganizationMDViewModel
    {
        [Required(ErrorMessage = "*This field is required.")]
        [CustomValidationRule(ErrorMessage = "*Another error message")]
        public string OrganizationNumber {get; set;}
    }
    

    Then you can do whatever you think will work in the getter and setter before mapping back to the domain model for persisting.

    For readability and easier debugging, you could do your ‘cleaning’ in the controller instead of in the getter and setter, but that’s up to you.

    [HttpPost]
    public ActionResult SaveData (OrganizationMDViewModel viewModel)
    {
      if (ModelState.IsValid) {
        viewModel.OrganizationNumber...//clean up the value here
        OrganizationMD = new OrganizationMD{OrganizationNumber = viewModel.OrganizationNumber}
        //...and save
      }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a problem that I suppose comes from my ASP.NET MVC project using
I have this ASP.net MVC project that requires a wizard like interface... So I
I'm using ELMAH to handle the exceptions in my ASP.Net MVC project. I would
I will start new project which is based on ASP.NET MVC 3 using C#
I have an existing complex website built using ASP.NET MVC, including a database backend,
I have an ASP .NET MVC 3 project and a problem with one of
I am currently working with an ASP.NET MVC 4 project, using C#. I would
I've been experimenting with ASP.Net MVC, and have come across a problem that is
I am using GIT with a new ASP.NET MVC project. I have a line
In my ASP.NET MVC project, I have a custom ModelMetadataProvider that requires a service

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.