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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T01:49:16+00:00 2026-06-01T01:49:16+00:00

I have a view where I use a dropdown list with enum: public enum

  • 0

I have a view where I use a dropdown list with enum:

public enum MaterialWorthEnumViewModel
{
    [Display(Name = "")] Undefined,
    [Display(Name = "< 1.000€")] LessThan1000,
    [Display(Name = "1.000€ < 10.000€")] Between1000And10000,
    [Display(Name = "10.000€ < 100.000€")] Between10000And100000,
    [Display(Name = "100.000€ < 25.000.000€")] Between100000And25000000,
    [Display(Name = "> 25.000.000€")] GreaterThan250000000,
}

I use a view model with this view:

public class MaterialEditNewViewModel
{
    public int RequestID { get; set; }
    ...
    [EnumRequired]
    public MaterialWorthEnumViewModel MaterialWorth { get; set; }
}

As you can see above, I used a custom validation [EnumRequired] I grab the code from a blog online.

[AttributeUsage(AttributeTargets.Parameter | AttributeTargets.Field | AttributeTargets.Property, AllowMultiple = false)]
public class EnumRequiredAttribute : RequiredAttribute    
{
    private const string UNDEFINED_VALUE = "Undefined";
    public string UndefinedValue { get; set; }

    public EnumRequiredAttribute() : this(UNDEFINED_VALUE)        
    {        }

    public EnumRequiredAttribute(string undefinedValue) : base()        
    {
        if (String.IsNullOrWhiteSpace(undefinedValue))            
        {
            throw new ArgumentNullException("undefinedValue");            
        }

        UndefinedValue = undefinedValue;        
    }         

    public override bool IsValid(object value)        
    {            
        if (value == null)            
        {                
            return false;            
        }             

        var undefined = Enum.Parse(value.GetType(), UndefinedValue);             
        return !Enum.Equals(value, undefined);        
    }    
}

Below is for the client side validation

public class ModelClientValidationEnumRequiredRule : ModelClientValidationRule 
{
    public ModelClientValidationEnumRequiredRule(string errorMessage, string undefinedValue) 
    { 
        base.ErrorMessage = errorMessage; 
        base.ValidationType = "enumrequired";
        base.ValidationParameters.Add("undefinedvalue", undefinedValue); 
    } 
}

public class EnumRequiredAttributeAdapter : DataAnnotationsModelValidator<EnumRequiredAttribute> 
{ 
    public EnumRequiredAttributeAdapter(ModelMetadata metadata, ControllerContext context, EnumRequiredAttribute attribute) 
        : base(metadata, context, attribute) 
    { } 

    public override IEnumerable<ModelClientValidationRule> GetClientValidationRules() 
    { 
        return new ModelClientValidationEnumRequiredRule[] 
        { 
            new ModelClientValidationEnumRequiredRule(base.ErrorMessage, Attribute.UndefinedValue) 
        }; 
    } 
}

Below is the javascript for the client side validation

Sys.Mvc.ValidatorRegistry.validators.enumrequired = function (rule) {
    var undefinedValue = rule.ValidationParameters.undefinedvalue;
    return function (value, context) {
        return value != undefinedValue;
    }
}

I also updated my GLobal.asax file:

DataAnnotationsModelValidatorProvider.RegisterAdapter(typeof(EnumRequiredAttribute), typeof(EnumRequiredAttributeAdapter)); 

The validation works pretty well on the server side but the client side validation is never triggered. So when I didn’t choose any value on my view for my dropdown enum, I reach the action in the controller and then the server side validation occured and I go back to the view. I concluded that the client side validation didn’t occurred.

Does someone can help me doing valid client side validation for this dropdown enum ?

Thanks. I’m a bit lost.

  • 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-01T01:49:17+00:00Added an answer on June 1, 2026 at 1:49 am

    I don’t see any relationship between your EnumRequiredAttribute and the other 2 classes. If you are using ASP.NET MVC 3 you need to associate your custom validation attribute with the adapter. This could be done in Application_Start:

    DataAnnotationsModelValidatorProvider.RegisterAdapter(
        typeof(EnumRequiredAttribute), 
        typeof(EnumRequiredAttributeAdapter)
    );
    

    Also on your client side you have shown some js code that relies on Microsoft*.js libraries. Those are now obsolete and should no longer be used. The default standard in ASP.NET MVC 3 for client side validation is the jquery.validate plugin.

    So let’s take an example.

    Model:

    public class MyViewModel
    {
        [EnumRequired]
        public MaterialWorthEnumViewModel MaterialWorth { get; set; }
    }
    

    Controller:

    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            return View(new MyViewModel());
        }
    
        [HttpPost]
        public ActionResult Index(MyViewModel model)
        {
            return View(model);
        }
    }
    

    View (Index.cshtml):

    @model MyViewModel
    <script src="@Url.Content("~/Scripts/jquery.validate.js")" type="text/javascript"></script>
    <script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.js")" type="text/javascript"></script>
    <script src="@Url.Content("~/Scripts/enumrequiredadapter.js")" type="text/javascript"></script>
    
    @using (Html.BeginForm())
    {
        @Html.LabelFor(x => x.MaterialWorth)
        @Html.EditorFor(x => x.MaterialWorth)
        @Html.ValidationMessageFor(x => x.MaterialWorth)
        <button type="submit">OK</button>
    }
    

    and finally the enumrequiredadapter.js adapter:

    (function ($) {
        $.validator.unobtrusive.adapters.add('enumrequired', ['undefinedvalue'], function (options) {
            options.rules['enumrequired'] = options.params;
            if (options.message != null) {
                options.messages['enumrequired'] = options.message;
            }
        });
    
        $.validator.addMethod('enumrequired', function (value, element, params) {
            return value != params.undefinedvalue;
        });
    
    })(jQuery);
    

    Also don’t forget to remove all traces of Microsoft*.js script references from your site. And that’s pretty much it.

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

Sidebar

Related Questions

I have the following view model: Public Class MyViewModel Public Property SelectedIDs As List(Of
I have a view in SQL Server 2008 that I want to use for
I use ClearCase. I have a snapshot view. Is there a way to compare
can i use fragments and map view in the same activity. I have seen
I use AutoMapper to map my domain objects to my view models. I have
I have a listview with 200 items. I use a custom view for each
In my view, if I have a situation where I need to use a
I have a view model that looks like this: public class VenueIndexViewModel : BaseViewModel
I am using Spring Web MVC for my application. I have 1 dropdown list
I am using DropDownListFor to render a dropdown list in a view. Somehow the

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.