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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T00:33:42+00:00 2026-05-27T00:33:42+00:00

My question is regarding MVC 2 custom validation. I’m stuck on a particular issue

  • 0

My question is regarding MVC 2 custom validation. I’m stuck on a particular issue and I’m unsure how to get around it. I’m fairly sure it’s more figuring out how to do it logically and then implementing it in code.

So what we have is a metadata class for a product. Each product has a product product ID which is the PK, and obviously unique. Each product also has a product code which is also unique. Customers enter the product code however, but the nature of the code ensures that only one code is attached to one product so it will be unique.

Here is a snippet from the metadata class:

 public partial class ProductMetadata
    {
        [DisplayName("Product Name")]
        [Required(ErrorMessage = "Product Name is required.")]
        public string ProductName { get; set; }

        [DisplayName("Product Code")]
        [Required(ErrorMessage = "Product Code is required.")]
        [ProductCodeAlreadyExistsValidator(ErrorMessage = "This Product code is in use.")]        
        public string ProductCode { get; set; }
}

The ‘ProductCodeAlreadyExistsValidator’ works perfectly when creating a new product. The problem lies in editing an existing product as the validation is being performed on this attribute again, and it is finding itself in the database. This results in the validation failing.

Here is a snippet from the custom validator:

public class ProductCodeAlreadyExistsValidator : ValidationAttribute
    {
        private readonly object typeId = new object();
        private const string defaultErrorMessage = "Product Code {0} is already present in the system.";

        public ProductCodeAlreadyExistsValidator()
            : base(defaultErrorMessage)
        {
        }

        public override object TypeId
        {
            get
            {
                return typeId;
            }
        }

        public string CustomerType { get; set; }
        public string CustomerFriendlyType { get; set; }

        public override string FormatErrorMessage(string roleName)
        {
            return String.Format(CultureInfo.CurrentUICulture, ErrorMessageString, roleName);
        }

        protected override ValidationResult IsValid(object value, ValidationContext validationContext)
        {
            if (!IsValid(value))
            {
                string errorMessage = string.Format(defaultErrorMessage, validationContext.MemberName, value as string);
                return new ValidationResult(errorMessage, new string[] { validationContext.MemberName });
            }

            return null;
        }

        public override bool IsValid(object value)
        {
            bool alreadyPresent = false;

            string ProductCode = value as string;

            using (ModelContainer ctn = new ModelContainer())
            {
                alreadyPresent = ctn.Products.Where(t => t.ProductCode == ProductCode).Count() > 0;
            }

            return !alreadyPresent;
        }
    }

It might be a relatively simple fix however I seem to have hit a brick wall with it. Can anyone offer any advice?

  • 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-27T00:33:43+00:00Added an answer on May 27, 2026 at 12:33 am

    Code seems ok to me. I think you need to identify if you are doing an insert or an update so the validation can ignore the checking when updating the field. You could check if the ID of the item editted is the same ID found by the code, it would be identified as a Editting.

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

Sidebar

Related Questions

I'm new to MVC and have a question regarding validation. Is there a way
I'm using MVC 2. I have a question regarding client and server side validation.
Greetings all... I have a simple, yet annoying question regarding Spring Web MVC. I
My question is regarding implementing MVC pattern in winforms I learned that the controller
I'm writing a simple blogging platform with ASP.NET MVC. My question is regarding forms
I am new to ASP.NET MVC and I have a question regarding viewing entity
Quick question regarding the use of Singleton lifestyle in Windsor, and Asp.Net MVC. If
This is a design question regarding an MVC implementation. I am creating a 2D
I have one question regarding using JQuery UI tab control in the asp.net mvc
I have a question regarding how to get the white- and black-listing feature of

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.