Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
95 views
in Technique[技术] by (71.8m points)

c# - Why does the code always go to the not false part upon returning the control to the request?

I have this call which calls a action in the controller and expects a true or false in the object named data.

$("#btnReview").click(function () {
    var data = {
        'id': '1',
        'recordID': selectedInspectionId, 'RoleRemarks': $("#CNGInspectionReport_RoleRemarks").val()
    };

    $.post('/CNGInspectionReport/ChangeStatus', data, function (data) {

        if (data == false) {

            alert('Something went wrong');
        }
        else {
            alert('Record reviewed successfully. Kindly review the further records, if any.');
           
        }
    });
});

and

public ActionResult ChangeStatus(int id, int recordID, string RoleRemarks, string Role = "") // Later, this should be converted to an object instead of parameters
{
    try
    {

        using (UnitOfWork uwork = new UnitOfWork())
        {
            CNGInspectionReportDAL = new CNGInspectionReportDAL();

            User user = (User)Session["User"];

            CNGInspectionReport CNGInspectionReport = uwork.CNGInspectionReportRepository.GetByID(recordID);

            CNGInspectionReport.CNGInspectionReportID = recordID;

            bool statusCrossOffice = false;

            if (id == 1) //Reviewed
            {
                if(user.Office.Trim() != CNGInspectionReport.StationName.Trim())
                {

                    return Json(new { data = statusCrossOffice, message = "Sorry, this record belongs to another office/station and can only be reviewed by the user of the same station/office" });
                }

                CNGInspectionReport.RoleRemarks = RoleRemarks;

                CNGInspectionReport.CheckedBy = user.UserID;
                CNGInspectionReport.CheckedByName = user.UserName;
                CNGInspectionReport.Status = (byte)id;
                CNGInspectionReport.ReviewDate = DateTime.Now;

            }
            return Json(new { data = status, message = "Success" });
        }
    }
    catch (Exception ex)
    {
        ViewBag.Error = ex.Message;
        return Json(new { data = false, message = ex.Message });
    }
}

but the problem is that it still goes to the else block when returns to the Ajax call. Why? I have clearly returned Fase in data but still it goes to the else part which is NOT FALSE.

question from:https://stackoverflow.com/questions/65891943/why-does-the-code-always-go-to-the-not-false-part-upon-returning-the-control-to

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You should evaluate your data property of the returned object

if (data.data == false) {
         alert('Something went wrong');
}

This is because the data returned value is an object and not a boolean. You can check the value yourself line this:

$.post('/CNGInspectionReport/ChangeStatus', data, function (data) {
         alert(JSON.stringify(data));
         // etc

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...