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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T08:30:27+00:00 2026-05-23T08:30:27+00:00

Context: creating a jsonP service with mvc controller methods which provides a definition of

  • 0

Context: creating a jsonP service with mvc controller methods which provides a definition of formfields including all validation rules.

My problem is that I do not know how to serialize the validation attributes. I prefer the validation attributes in the same format as they are serialized by Razor when using unobtrusive validation in regular Mvc views.

For serializing to json I use NewtonSoft.Json (4.0.2).

Example of model:
public class Profile{

    [Required(ErrorMessage="This field is required.")]
    [StringLength(25, ErrorMessage="Max 25 chars.")]
    public string Firstname{get;set;}
    }

Example of preferred serialized javascript:

     {"Firstname": "John", 
      "ValidationRules":[{"data-val-required":"This field is required.", "data-val-length-max":25, "data-val-length":"Max 25 chars." }]}

Any help or pointers are very much appreciated.

  • 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-23T08:30:27+00:00Added an answer on May 23, 2026 at 8:30 am

    This will construct a dictionary with the validation attributes for a given property based on the data annotation attributes:

    var metadata = ModelMetadataProviders.Current.GetMetadataForProperty(null, typeof(MyModel), "MyProperty");
    var validationRules = metadata.GetValidators(ControllerContext).SelectMany(v => v.GetClientValidationRules());
    var validationAttributes = new Dictionary<string, string>();
    
    foreach (ModelClientValidationRule rule in validationRules)
    {
        string key = "data-val-" + rule.ValidationType;
        validationAttributes.Add(key, HttpUtility.HtmlEncode(rule.ErrorMessage ?? string.Empty));
        key = key + "-";
        foreach (KeyValuePair<string, object> pair in rule.ValidationParameters)
        {
            validationAttributes.Add(key + pair.Key,
                HttpUtility.HtmlAttributeEncode(
                    pair.Value != null ? Convert.ToString(pair.Value, CultureInfo.InvariantCulture) : string.Empty));
        }
    }
    

    You should then serialize the validationAttributes dictionary with your property in your custom JSON serialization code.

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

Sidebar

Related Questions

Currently my method involves creating a context processor which is then included sitewide. However,
I am creating an NSURLMutableRequest and setting HTTP methods and content-types and such. All
In the context of creating a custom Eclipse distribution for a development team. How
I am using XML file for creating Context Menu for my ListView. (Please see
I’m creating a UserControl for a rich TreeView (one that has context menus for
I'm creating a WPF app and have a system tray icon with a context
Working inside the context of an ASP.NET application I am creating a page that
I have a WCF REST Service which accepts a JSON string One of the
I'm creating a gallery section for my app which will be used to show
I understand the purpose of events, especially within the context of creating user interfaces.

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.