Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
327 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

Please log in or register to reply this article.

OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...