I am having inconsistent behavior with an Ajax call.
Controller...
public JsonResult checkWwid(string wwid)
{
EMPLOYEE employee = db.EMPLOYEES.SingleOrDefault(e => e.ID == wwid.ToUpper());
if (employee != null)
{
return Json(employee, JsonRequestBehavior.AllowGet);
}
employee = new EMPLOYEE();
employee.ID = "Unknown";
employee.NAME = "Unknown";
employee.ORGANIZATION_ID = "Unknown";
employee.WORK_SITE_ID = "Unknown";
return Json(employee, JsonRequestBehavior.AllowGet);
}
When a user enters an employee ID that isn't recognized, I return an employee object that has all values of "Unknown". This works fine. However, when the employee ID entered matches a result in the database, for some reason, the "else" condition does not get reached--despite the fact the an employee Json object is successfully being returned to the view.
View...
$('#Wwid').change(function () {
$("#wwid_alert").hide();
var selectedWwid = $(this).val();
$.getJSON('@Url.Action("checkWwid")', { wwid: selectedWwid }, function (employee) {
alert(employee.NAME);
if (employee.ID == 'Unknown') {
alert(employee.NAME);
$("#wwid_alert").show();
$("#wwid_alert").text("This WWID is not in the database; you will not be able to sucessfully submit this form.");
} else { // This is not working even though I am getting a JSON result.
alert(employee.NAME);
$("#wwid_alert").show();
$("#wwid_alert").text(employee.NAME);
}
});
});
Through debugging, I know the Json result is being returned to the View. I have used this same algorithm in another application (that uses the exact same database), and that works fine. I am at a loss for how to solve this. Any suggestions or recommendations would be valued greatly. Thank you.