Context: creating a jsonP service using the mvc controller methods, which provides the definition of form fields, including all validation rules.
My problem is that I do not know how to serialize validation attributes. I prefer validation attributes in the same format as Razor serialized when using unobtrusive validation in regular Mvc views.
For serialization in json, I use NewtonSoft.Json (4.0.2).
Model Example: Public Class Profile {
[Required(ErrorMessage="This field is required.")] [StringLength(25, ErrorMessage="Max 25 chars.")] public string Firstname{get;set;} }
Preferred serialized javascript example:
{"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 greatly appreciated.
bees73 source share