0

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.

tonyapolis
  • 201
  • 2
  • 12

2 Answers2

1

hi you can also get your json value like this it's below

 $.ajax({
            url: "/City/State",
            dataType: 'json',
            data: { countryId: countryID },
            success: function (data) {
                alert(data);
                $("#stateID").empty();
                $("#stateID").append("<option value='0'>--Select State--</option>");
                $.each(data, function (index, optiondata) {
                    alert(optiondata.StateName);
                    $("#stateID").append("<option value='" + optiondata.ID + "'>" + optiondata.StateName + "</option>");
                });
            },
            error: function () {
                alert('Faild To Retrieve states.');
            }

        });

try this i think this will help you

Rajpurohit
  • 1,951
  • 2
  • 16
  • 19
0

Now I'm embarrassed; I hadn't debugged completely. I thought that because the controller had returned control to the view that a Json result was being returned. However, after debugging in Firebug I discovered that I was getting a Http 500 error, likely due to the way the Entity Framework object was being serialized by ASP.NET's built-in Json serializer.

Rather than sending the EF object as a Json result, I instantiated a new object and assigned only the properties I needed for the result.

Maybe I have helped someone else solve an issue in the future. :)

Additionally, the following post helped...

Community
  • 1
  • 1
tonyapolis
  • 201
  • 2
  • 12