首页 > 解决方案 > 尽管 if 条件为假,但仍执行语句

问题描述

让我换个方式问这个问题,有没有办法检查 BID=0 是否将其转换为 null else //do stuff

我正在旧代码上添加新功能。当验证满足时,数据通过 ajax 从视图发送到控制器。数据发送正确,问题出在控制器中,因为我添加了 if...else。据说

if(BID == 0)
{
//implement something
}
else
{
//implement another thing
}

问题是条件是假还是真,第一条语句被执行,我不知道为什么。有任何想法吗?

这是ajax

function SubmitTermination() {
        chkVisibility()
        validate();
        if (EmployeeIsValid && bossIdvalid && ResignationDateIdvalid && PhoneIsvalid) {
            var EmployeeID = $('#EmpID').val();
            var BossID = $('#MangrID').val();
            var RDDate = $('#resignationDate').val();
            var RDReason = $('#TerminationType').val();
            var RDReasonDetail = $('#terminationDescription').val();
            var phone = $('#EmployeePhone').val();
            var remember = document.getElementById('IsAgent');
            var LDDate = $('#LeavingDate').val();

            var isAgent = false;
            if (remember.checked) {
                isAgent = true;
            } else {
                isAgent = false;
            }
            var data = {
                UserId: EmployeeID,
                BID: BossID,
                TDate: RDDate,
                LDate: LDDate,
                TReason: RDReason,
                TPhoneNum: phone,
                Agent: isAgent,
                TReasonDetail: RDReasonDetail
            };

            $.ajax({
                type: 'POST',
                url: '/Resignation/SaveTermination',
                daatype: 'json',
                data: data,
                success: function (result) {
                    document.getElementById('statusResult').textContent = result;
                    $('#PopUP').modal('show');
                    setTimeout(location.reload.bind(location), 4000);

                }
            });
        }
        else if (EmployeeIsValid && !bossIdvalid && BossEmpty && ResignationDateIdvalid && PhoneIsvalid) {
            var EmployeeID = $('#EmpID').val();
            var RDDate = $('#resignationDate').val();
            var RDReason = $('#TerminationType').val();
            var RDReasonDetail = $('#terminationDescription').val();
            var phone = $('#EmployeePhone').val();
            var remember = document.getElementById('IsAgent');
            var LDDate = $('#LeavingDate').val();

            var isAgent = false;
            if (remember.checked) {
                isAgent = true;
            } else {
                isAgent = false;
            }
            var data = {
                UserId: EmployeeID,
                BID: 0,
                TDate: RDDate,
                LDate: LDDate,
                TReason: RDReason,
                TPhoneNum: phone,
                Agent: isAgent,
                TReasonDetail: RDReasonDetail
            };

            $.ajax({
                type: 'POST',
                url: '/Resignation/SaveTermination',
                daatype: 'json',
                data: data,
                success: function (result) {
                    document.getElementById('statusResult').textContent = result;
                    $('#PopUP').modal('show');
                    setTimeout(location.reload.bind(location), 4000);

                }
            });

        }
    }

这是有问题的控制器

        public JsonResult SaveTermination(int UserId, int BID, string TDate, string LDate, int TReason, Int32 TPhoneNum, bool Agent, string TReasonDetail = "No Details")
        {
            string result = "nothign yet";
            //my try inside the original
            #region transfer
//the issue is here 
            if (BID == 0)
            {

                bool userInDb = userCheck.checkUserInDB(UserId);
                bool BossInDb = false;
                bool userAddFromOracle = false;
                bool resignationDateCheck = false;
                bool leavingDateCheck = false;
                bool exsistingResignations = false;
                bool ResignationsCount = false;

                ExitApplication.Models.User user;
                using (db = new ExitApplication2015Entities())
                {
                    user = db.Users.Where(u => u.UserID == UserId).SingleOrDefault();
                }

                #region Check users in DBS
                if (!userInDb)
                {
                    userAddFromOracle = userCheck.GetUserDataFromOracle(UserId.ToString());
                    if (!userAddFromOracle)
                    {
                        result = "Could not find Your User in database or in oracle";
                        userInDb = false;
                    }
                    else
                    {
                        userInDb = true;
                    }
                }

                #endregion

                DateTime resignDate = DateTime.ParseExact(TDate, "MM/dd/yyyy", CultureInfo.InvariantCulture);
                DateTime leavDate = DateTime.ParseExact(LDate, "MM/dd/yyyy", CultureInfo.InvariantCulture);

                #region Check the Resignation Date

                if (resignDate >= DateTime.Now.Date)
                {
                    resignationDateCheck = true;
                }

                #endregion

                #region Check the Leaving Date

                if (leavDate >= DateTime.Now.Date)
                {
                    leavingDateCheck = true;
                }

                #endregion

                var resdt = new Resignation();
                using (db = new ExitApplication2015Entities())
                {
                    resdt = db.Resignations.Where(r => r.EmployeeID == UserId && r.OpsRetained == false && r.IsCanceled == false && r.IsCompleted == false && r.IsRejected == false && r.HRRetained == false && r.IsTerminated == false).DefaultIfEmpty().SingleOrDefault();

                }

                #region Previous Resignation check
                if (resdt == null)
                {
                    exsistingResignations = false;
                }
                else
                {
                    exsistingResignations = true;
                }
                #endregion

                var LeavDateCount = 0;
                using (db = new ExitApplication2015Entities())
                {
                    LeavDateCount = db.Resignations.Count(r => r.LeavDate == leavDate);
                }

                #region Leav Date Count Check

                if (LeavDateCount < 30)
                {
                    ResignationsCount = true;
                }
                else
                {
                    ResignationsCount = false;
                }

                #endregion



                if (userInDb && !BossInDb && BID == 0 && !exsistingResignations && ResignationsCount && leavingDateCheck)
                {
                    Resignation ResignApplication = new Resignation
                    {
                        EmployeeID = UserId,
                        ResignStatusID = 3,
                        ResignDate = resignDate,
                        LeavDate = leavDate,
                        IsActive = true,
                        IsRejected = false,
                        OpsRetained = false,
                        HRRetained = false,
                        IsTerminated = false,
                        IsCanceled = false,
                        IsCompleted = false,
                        insertedBy = "System",
                        insertedOn = DateTime.Now,
                        DepartmentID = user.UserDepartmentID,
                        AccountOrOUID = (user.UserAccountOUID)

                    };

                    using (db = new ExitApplication2015Entities())
                    {
                        db.Resignations.Add(ResignApplication);
                        db.Entry<Resignation>(ResignApplication).State = EntityState.Added;
                        db.SaveChanges();
                    }

                    int resignID = ResignApplication.ResignID;

                    ResignationDetail resignDetails = new ResignationDetail
                    {
                        ResignID = resignID,
                        ResignReason1 = TReason,
                        ResignReasonDetail1 = TReasonDetail
                    };
                    using (db = new ExitApplication2015Entities())
                    {
                        db.ResignationDetails.Add(resignDetails);
                        db.Entry<ResignationDetail>(resignDetails).State = EntityState.Added;
                        db.SaveChanges();
                    }
                    List<ConcernedParty> ConcernedParties;

                    using (db = new ExitApplication2015Entities())
                    {
                        ConcernedParties = db.ConcernedParties.Where(g => g.IsActive == true).ToList();
                    }

                    foreach (ConcernedParty ConcernedParty in ConcernedParties)
                    {
                        ResignReleaseDetail relDet = new ResignReleaseDetail
                        {
                            /*@@*/
                            ResignID = resignID,
                            ConcernedPartyID = ConcernedParty.ConcernedPartyID,
                            ReleaseStatusID = 3,
                            InsertedBy = Users.CurrentUserId.ToString(),
                            InsertedOn = DateTime.Now,
                            IsActive = true
                        };

                        using (db = new ExitApplication2015Entities())
                        {
                            db.ResignReleaseDetails.Add(relDet);
                            db.Entry<ResignReleaseDetail>(relDet).State = EntityState.Added;
                            db.SaveChanges();
                        }

                    }
                    try
                    {
                        // Mailing mail = new Mailing(ResignApplication);
                        // mail.sendMail(1, Int32.Parse(BID.ToString()));
                        result = "Resignation Submitted Sucessfully";
                    }
                    catch
                    {
                        result = "The Resignation was submitted but the emails were not sent properly,please contact concerned parties on the mail.";
                    }

                }
                else
                {
                    if (!userInDb)
                    {
                        result = "Sorry Could not Find your User ID in System database or Oracle database";
                    }
                    if (!resignationDateCheck)
                    {
                        result = "Sorry you Can not submit a resination with a resign date older than today";
                    }
                    if (exsistingResignations)
                    {
                        result = "Sorry you have a pending Termination please follow up on it";
                    }

                    if (!leavingDateCheck)
                    {
                        result = "Sorry you Can not submit a resignation with a Leaving date older than resignation date";
                    }

                    if (!ResignationsCount)
                    {
                        result = "Resignation Capacity is 30 per day.Sorry you have to choose another Monday or Thursday";
                    }
                }
                return Json(result);
            }
            #endregion




            else
            {
                //string result = "nothign yet";
                bool userInDb = userCheck.checkUserInDB(UserId);
                bool BossInDb = userCheck.checkUserInDB(BID);
                bool userAddFromOracle = false;
                bool BossAddFromOracle = false;
                bool resignationDateCheck = false;
                bool leavingDateCheck = false;
                bool exsistingResignations = false;
                bool ResignationsCount = false;

                ExitApplication.Models.User user;
                using (db = new ExitApplication2015Entities())
                {
                    user = db.Users.Where(u => u.UserID == UserId).SingleOrDefault();
                }

                #region Check users in DBS
                if (!userInDb)
                {
                    userAddFromOracle = userCheck.GetUserDataFromOracle(UserId.ToString());
                    if (!userAddFromOracle)
                    {
                        result = "Could not find Your User in database or in oracle";
                        userInDb = false;
                    }
                    else
                    {
                        userInDb = true;
                    }
                }
                if (!BossInDb)
                {
                    BossAddFromOracle = userCheck.GetUserDataFromOracle(BID.ToString());
                    if (!BossAddFromOracle)
                    {
                        result = "Could not find Your Boss User in database or in oracle";
                        BossInDb = false;
                    }
                    else
                    {
                        BossInDb = true;
                    }
                }
                #endregion

                DateTime resignDate = DateTime.ParseExact(TDate, "MM/dd/yyyy", CultureInfo.InvariantCulture);
                DateTime leavDate = DateTime.ParseExact(LDate, "MM/dd/yyyy", CultureInfo.InvariantCulture);

                #region Check the Resignation Date

                if (resignDate >= DateTime.Now.Date)
                {
                    resignationDateCheck = true;
                }

                #endregion

                #region Check the Leaving Date

                if (leavDate >= DateTime.Now.Date)
                {
                    leavingDateCheck = true;
                }

                #endregion

                var resdt = new Resignation();
                using (db = new ExitApplication2015Entities())
                {
                    resdt = db.Resignations.Where(r => r.EmployeeID == UserId && r.OpsRetained == false && r.IsCanceled == false && r.IsCompleted == false && r.IsRejected == false && r.HRRetained == false && r.IsTerminated == false).DefaultIfEmpty().SingleOrDefault();

                }

                #region Previous Resignation check
                if (resdt == null)
                {
                    exsistingResignations = false;
                }
                else
                {
                    exsistingResignations = true;
                }
                #endregion

                var LeavDateCount = 0;
                using (db = new ExitApplication2015Entities())
                {
                    LeavDateCount = db.Resignations.Count(r => r.LeavDate == leavDate);
                }

                #region Leav Date Count Check

                if (LeavDateCount < 30)
                {
                    ResignationsCount = true;
                }
                else
                {
                    ResignationsCount = false;
                }

                #endregion



                if (userInDb && BossInDb && !exsistingResignations && ResignationsCount && leavingDateCheck)
                {
                    Resignation ResignApplication = new Resignation
                    {
                        EmployeeID = UserId,
                        ManagerID = BID,
                        ResignStatusID = 1,
                        ResignDate = resignDate,
                        LeavDate = leavDate,
                        IsActive = true,
                        IsRejected = false,
                        OpsRetained = false,
                        HRRetained = false,
                        IsTerminated = false,
                        IsCanceled = false,
                        IsCompleted = false,
                        insertedBy = "System",
                        insertedOn = DateTime.Now,
                        DepartmentID = user.UserDepartmentID,
                        AccountOrOUID = (user.UserAccountOUID)

                    };

                    using (db = new ExitApplication2015Entities())
                    {
                        db.Resignations.Add(ResignApplication);
                        db.Entry<Resignation>(ResignApplication).State = EntityState.Added;
                        db.SaveChanges();
                    }

                    int resignID = ResignApplication.ResignID;

                    ResignationDetail resignDetails = new ResignationDetail
                    {
                        ResignID = resignID,
                        ResignReason1 = TReason,
                        ResignReasonDetail1 = TReasonDetail
                    };
                    using (db = new ExitApplication2015Entities())
                    {
                        db.ResignationDetails.Add(resignDetails);
                        db.Entry<ResignationDetail>(resignDetails).State = EntityState.Added;
                        db.SaveChanges();
                    }
                    List<ConcernedParty> ConcernedParties;

                    using (db = new ExitApplication2015Entities())
                    {
                        ConcernedParties = db.ConcernedParties.Where(g => g.IsActive == true).ToList();
                    }

                    foreach (ConcernedParty ConcernedParty in ConcernedParties)
                    {
                        ResignReleaseDetail relDet = new ResignReleaseDetail
                        {
                            /*@@*/
                            ResignID = resignID,
                            ConcernedPartyID = ConcernedParty.ConcernedPartyID,
                            ReleaseStatusID = 3,
                            InsertedBy = Users.CurrentUserId.ToString(),
                            InsertedOn = DateTime.Now,
                            IsActive = true
                        };

                        using (db = new ExitApplication2015Entities())
                        {
                            db.ResignReleaseDetails.Add(relDet);
                            db.Entry<ResignReleaseDetail>(relDet).State = EntityState.Added;
                            db.SaveChanges();
                        }

                    }
                    try
                    {
                        // Mailing mail = new Mailing(ResignApplication);
                        // mail.sendMail(1, Int32.Parse(BID.ToString()));
                        result = "Resignation Submitted Sucessfully";
                    }
                    catch
                    {
                        result = "The Resignation was submitted but the emails were not sent properly,please contact concerned parties on the mail.";
                    }

                }
                else
                {
                    if (!userInDb)
                    {
                        result = "Sorry Could not Find your User ID in System database or Oracle database";
                    }
                    if (!BossInDb)
                    {
                        result = "Sorry Could not Find your Direct Manager ID in System database or Oracle database";
                    }
                    if (!resignationDateCheck)
                    {
                        result = "Sorry you Can not submit a resination with a resign date older than today";
                    }
                    if (exsistingResignations)
                    {
                        result = "Sorry you have a pending Termination please follow up on it";
                    }

                    if (!leavingDateCheck)
                    {
                        result = "Sorry you Can not submit a resignation with a Leaving date older than resignation date";
                    }

                    if (!ResignationsCount)
                    {
                        result = "Resignation Capacity is 30 per day.Sorry you have to choose another Monday or Thursday";
                    }
                }
                return Json(result);
            }
        }

标签: c#asp.net-mvcif-statementintasp.net-ajax

解决方案


推荐阅读