javascript - asp.net mvc json.net response -
i implementing, first time, json.net asp.net mvc2 site.
my original code looked this:
[httppost] public actionresult findme(string searchfirstname, string searchlastname) { this.searchfirstname = searchfirstname; this.searchlastname = searchlastname; ienumerable<homepageuser> results = dosearch(); bool success = (results.count() == 0) ? false : true; return json(new { success = success, results = results }); }
the results problematic due fact 1 of items in results set enum , want text value, not numeric one. additionally, date format problem.
so, found json.net , changed code this:
[httppost] public jsonnetresult findme(string searchfirstname, string searchlastname) { this.searchfirstname = searchfirstname; this.searchlastname = searchlastname; ienumerable<homepageuser> results = dosearch(); bool success = (results.count() == 0) ? false : true; jsonnetresult jsonnetresult = new jsonnetresult(); jsonnetresult.serializersettings.converters.add(new isodatetimeconverter()); jsonnetresult.data = results;// how add success true/false info? return jsonnetresult; }
this fixes above 2 issues, i'm wondering how make seamless existing javascript code expecting json looked like:
{ "success":true, "results":[{ "userid":545, "firstname":"scott", "lastname":"roberson"}] }
this allowed me first test response.success before proceeding write out answer, versus moving section handle errors.
so, question how add top-level success json node live beside results node?
thanks.
update:
as case, act of writing out question sparked idea in 1 of doh! moments.
if add:
var returnpackage = new { success = success, results = results};
then add jsonnetresult.data so:
jsonnetresult.data = returnpackage;
it works perfectly.
thanks, anyway.
final code:
[httppost] public jsonnetresult findme(string searchfirstname, string searchlastname) { this.searchfirstname = searchfirstname; this.searchlastname = searchlastname; ienumerable<homepageuser> results = dosearch(); bool success = (results.count() == 0) ? false : true; var returnpackage = new { success = success, results = results}; jsonnetresult jsonnetresult = new jsonnetresult(); jsonnetresult.serializersettings.converters.add(new isodatetimeconverter()); jsonnetresult.data = returnpackage; return jsonnetresult; }
here's in mvc/ajaxy apps. define new class:
public class jsonresultdata { private bool _success = true; public bool success { { return _success; } set { _success = value; } } public object value { get; set; } public list<string> errors { get; set; } public jsonresultdata() { this.errors = new list<string>(); } }
then set return data value to:
jsonnetresult.data = new jsonresultdata { value = results };
jsonresultdata.success property want test in json response first. defaults true, if goes wrong in server-side method, set false , add error message, this:
catch (exception ex) { list<string> errors = new list<string>(); errors.add(ex.tostring()); jsonnetresult.data = new jsonresultdata { success = false, errors = errors }; }
Comments
Post a Comment