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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T06:02:38+00:00 2026-05-26T06:02:38+00:00

I understood how to create custom validation attributes. In fact, the Validate method is

  • 0

I understood how to create custom validation attributes. In fact, the Validate method is being execute when I use the Seed method to pre-populate the database, and if it fails it throws an exception.
However, the validation is not working on the Create Form of the entity.

Do I have to change something to the HTML (Razor Form)?

It just lets me add items that fail the validation.

Code here:

namespace Data.Model
{
    [AttributeUsage(AttributeTargets.Property | AttributeTargets.Field, AllowMultiple = false)]
    sealed public class YearsValidationAttribute : ValidationAttribute
    {
        // Internal field to hold the min value.
        readonly int _years;

        public int Years
        {
            get { return _years; }
        }

        public YearsValidationAttribute(int years)
        {
            _years = years;
        }


        public override bool IsValid(object value)
        {
            var years = (int)value;
            bool result = true;
            if (this.Years != null)
            {
                result = Years >= years;
            }
            return result;
        }



        public override string FormatErrorMessage(string name)
        {
            return String.Format(CultureInfo.CurrentCulture,
              ErrorMessageString, name, Years);
        }
    }
}


public class Position
    {
        [DatabaseGenerated(System.ComponentModel.DataAnnotations.DatabaseGeneratedOption.Identity)]   
        public int PositionID { get; set; }

        [Required(ErrorMessage = "Position name is required.")]
        [StringLength(20, MinimumLength = 3, ErrorMessage = "Name should not be longer than 20 characters.")]
        [Display(Name = "Position name")]              
        public string name { get; set; }

        [Required(ErrorMessage = "Number of years is required")] 
        [Display(Name = "Number of years")]
        [YearsValidationAttribute(5, ErrorMessage = "{0} value must be greater than {1} years.")]        
        public int yearsExperienceRequired { get; set; }

        public virtual ICollection<ApplicantPosition> applicantPosition { get; set; }
    }



@using (Html.BeginForm()) {
    @Html.ValidationSummary(true)
    <fieldset>
        <legend>Position</legend>

        @Html.HiddenFor(model => model.PositionID)

        <div class="editor-label">
            @Html.LabelFor(model => model.name)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.name)
            @Html.ValidationMessageFor(model => model.name)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.yearsExperienceRequired)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.yearsExperienceRequired)
            @Html.ValidationMessageFor(model => model.yearsExperienceRequired)
        </div>

        <p>
            <input type="submit" value="Save" />
        </p>
    </fieldset>
}
  • 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-26T06:02:38+00:00Added an answer on May 26, 2026 at 6:02 am

    That code you have won’t give you a client validator, only the server side. But that is a good starting point. What you need to do in your action method on the server is to check if the model is valid like such:

    public ActionResult YourAction(YourModel model)
    {
        if(ModelState.IsValid)
        {
            // Do your save
        }
        else
        {
            // Do your other stuff
        }
    }
    

    If you want client side validation as well you can use the resources here: http://bradwilson.typepad.com/blog/2010/10/mvc3-unobtrusive-validation.html

    You can also try to use the Remote validation attribute: http://msdn.microsoft.com/en-us/library/gg508808(v=vs.98).aspx

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

Sidebar

Related Questions

If you like to create custom delegates you would use the delegate keyword in
After being taught how to create a hash table in class, I don't understand
I understand how to create a custom Android control, and I believe I understand
While researching how to create custom compound views in Android, I have come across
I have create a custom UIWebView. On some class if I include my custom
I am trying to create a custom control which does nothing except decorate the
I think I know how to create custom encrypted RSA keys, but how can
I need to create a custom type for NHibernate by writing a new mapper
I'm trying to create a custom exception handler for the exception handling block of
im trying to create a custom macro program in C# and I want to

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.