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

2
  • 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. Commented Dec 2, 2013 at 17:41
  • I think you are for this answer Commented Dec 9, 2013 at 8:16

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.