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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T05:00:40+00:00 2026-05-31T05:00:40+00:00

I developed my own custom htmlhelper for managing my radio buttons. No problems for

  • 0

I developed my own custom htmlhelper for managing my radio buttons. No problems for generating basic html tag from my custom helper. But I have problems for injecting validation attributes in my html tag (client side unobtrusive validation). I used the htmlHelper.GetUnobtrusiveValidationAttributes(prefix) for retrieving validation attributes from my model (data annotations) but it doesn’work for my custom RequiredAttribute.

Here is a part of my view model:

public class MaterialEditNewViewModel
{
    public int RequestId { get; set; }

    [CustomRequired]
    Public bool ADR { get; set; }
    ...
}

Here is my CustomRequired:

public class CustomRequiredAttribute : RequiredAttribute
{        
    public override string FormatErrorMessage(string name)
    {
        string translatedFieldName = UserResource.ResourceManager.GetString(name);
        if (string.IsNullOrWhiteSpace(translatedFieldName))
            translatedFieldName = name;
        return string.Format(UserResource.FieldRequired, translatedFieldName);
    }   
}

Here is my custom html helper:

public static IHtmlString RadioButtonFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression, object value, string labelText)
    {
        var metadata = ModelMetadata.FromLambdaExpression(expression, htmlHelper.ViewData);
        string prefix = ExpressionHelper.GetExpressionText(expression);

        var validationAttributes = htmlHelper.GetUnobtrusiveValidationAttributes(prefix); 

        object currentValue = metadata.Model;
        string property = metadata.PropertyName;

        // Build the radio button html tag 
        var htmlRadio = new TagBuilder("input");
        htmlRadio.GenerateId(property + value);
        htmlRadio.Attributes["type"] = "radio";
        htmlRadio.Attributes["name"] = property;
        htmlRadio.Attributes["value"] = Convert.ToString(value);

        foreach (KeyValuePair<string, object> pair in validationAttributes)
        {
            htmlRadio.MergeAttribute(pair.Key, pair.Value.ToString());
        } 

        if (object.Equals(currentValue, value))
        {
            htmlRadio.Attributes["checked"] = "checked";
        }

        // Build the label html tag 
        var label = new TagBuilder("label");
        label.Attributes["for"] = htmlRadio.Attributes["id"];
        label.SetInnerText(labelText);

        // Return the concatenation of both tags 
        return new HtmlString(htmlRadio.ToString(TagRenderMode.SelfClosing) + label.ToString()
        );
    }
  1. You have to know that my CustomRequired data annotation works for basic helper like @Html.RadioButtonFor(model => model.ADR) but it doesn’t work when used on custom htmlhelpers.

  2. I know that when I use ‘classic’ data annotation like [Required] in my model I have no problems retrieving validation attributes BUT when using my CustomRequiredAttribute the GetUnobtrusiveValidationAttributes returns nothing!

Any idea why? If I’m not clear, don’t hesitate to ask me to clarify.

I forgot to say that I use Entity Framework Code First with MVC3.

Thanks.

  • 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-31T05:00:42+00:00Added an answer on May 31, 2026 at 5:00 am

    Since the RequiredAttribute doesn’t implement the IClientValidatable interface you must register a custom adapter for your custom attribute. You could do this by adding the following line in Application_Start:

    DataAnnotationsModelValidatorProvider.RegisterAdapter(
        typeof(CustomRequiredAttribute), 
        typeof(RequiredAttributeAdapter)
    );
    

    There are 2 ways to implement client side validation attributes:

    • Have your custom validation attribute implement the IClientValidatable interface
    • Register a custom adapter

    The RequiredAttribute uses the second approach. It associates the RequiredAttributeAdapter to all required attributes. But since you have derived from the RequiredAttribute, your custom attribute no longer has the adapter. So you need to register it in Application_Start.

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

Sidebar

Related Questions

We have our own custom calendar in our portal which is developed in Java.
I've developed my own custom users and roles objects using ActiveRecord that do NOT
We have an ASP.NET MVC application for which we have developed our own custom
I have developed an app which uses my own custom keyboard (well, a view
I developed my own custom web server for a robot I built ( video
I'm working with a CMS which allows you to develop your own custom controls
If we have developed our own ORM framework and the framework is working fine
I am web developer who currently works mostly on his own but, some projects
I've developed my own framework for iPhone. Among my classes, I have classes to
Is it possible to create a custom Web front-end to run SSRS reports from?

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.