I am using an AJAX call to display information based on success or failure of some logic written in C#. I now need to return additional data back from the server and display it. The data I need is contained in the employers and agencies variables. How can I return that data in my return Json statement along with the success?
$.ajax({
url: rootpath + "/clients/hasDuplicateTaxId",
type: "GET",
success: function (data) {
if (data.success) {
// success
}
else {
// fail
}
},
error: function (response) {
var error = response;
}
});
if (taxIdExists)
{
var employers = _employerRepository.GetByTaxId(taxId).ToList();
var agencies = _employerRepository.GetByTaxId(taxId).Select(e => e.GeneralAgency).ToArray();
return Json(new { success = true }, JsonRequestBehavior.AllowGet);
}
return Json(new { success = false, error = "Error" }, JsonRequestBehavior.AllowGet);
employersandagenciesvariables to the anonymous type you provide to theJsonresponse. You can then access the properties/objects/arrays they expose in thedatavariable of your JavaScript. It's impossible to give you anything more concrete than that, as you haven't shown us what those classes actually look like.errormember in the C# code? Do the same thing for the other ones too.