json - Problem with a simple jQuery Templates example -
i using jquery-tmpl
, receiving following json
data, using form validation in asp.net mvc3:
{"status":1,"message":"oh dear, have done. check list of errors dude!","errors":["the contents field required.","the date created field required.","the date updated field required.","the updated field required."]}
my template looks this:
<script id="responsetemplate" type="text/x-jquery-tmpl"> {{each(i, error) errors}} <li>${error}</li> {{/each}} </script>
my json
post follows:
var data = { contents: "this test", datecreated: "", dateupdated: "", updatedby: "ben" }; $.ajax({ url: '@url.action("save", "note")', data: json.stringify(data), type: 'post', contenttype: 'application/json', datatype: 'json', success: function (result) { alert(result.errors); $("#responses").tmpl(result).appendto("#responsetemplate") } });
the data correct. alert showing errors array string. template not working.
the answer must simple.
i think have responses
, responsetemplate
mixed up:
$("#responsetemplate").tmpl(result).appendto("#responses");
here's example (without ajax calls): http://jsfiddle.net/andrewwhitaker/gcqzx/
you write template this:
<script id="responsetemplate" type="text/x-jquery-tmpl"> <li>${$data}</li> </script>
and call this:
$("#responsetemplate").tmpl(result.errors).appendto("#responses");
(not sure if it's better, wanted point out don't have use {{each}}
in case).
Comments
Post a Comment