2

I'm unable to figure out the correct JSON object I need to pass into jQuery validation's showErrors() method. So on the client I have JavaScript:

  var validator = $("form").validate();
  validator.showErrors(modelstate_json);

And server-side extracting ModelState errors into JSON is easily done

var errors = (from e in ModelState where e.Value.Errors.Count > 0 select e)
ToDictionary(x => 
 x.Key, 
 x => x.Value.Errors
.Select(y => y.ErrorMessage)
.Concat(x.Value.Errors.Where(y => y.Exception != null)
.Select(y => y.Exception.Message)));

JsonConvert.SerializeObject(errors);

Which gives me something like

'{"Name": "Max 30 characters"}'

But passing this fragment to showErrors() does not trigger validation and display the message.

If I know the correct JSON to feed showErros() then I am hoping I can reverse engineer my serialization routine.

Thanks

puri
  • 1,829
  • 5
  • 23
  • 42
  • This is backwards... you don't pass messages ***into*** `showErrors()`. The error messages come ***from*** `showErrors()`. The jQuery Validation plugin automatically triggers errors based on the user's data input and the two arguments for `showErrors()` come from the plugin itself. [See documentation](http://jqueryvalidation.org/validate/). – Sparky Dec 02 '13 at 17:41
  • I think you are for [this answer](http://stackoverflow.com/a/14106135/894273) – Dmitry Efimenko Dec 09 '13 at 08:16

0 Answers0