ASP.Net MVC如果条款被忽略

时间:2017-10-27 09:52:21

标签: c# asp.net asp.net-mvc

我的if子句被忽略了,即使被比较的变量有一个值,如果它有一个值并且它是真的,那么它会做一些事情,但是程序甚至没有阅读条款,它只是跳跃,我看到了一个断点。

这是我的控制器:

public ActionResult Delete(int? id)
    {
        string result = null;
        try
        {
            if (id == null)
            {
                result = HIQResources.errorMessageUnknownAction;
                return new JsonResult { Data = result };
            }    
            StudentViewModel vm = new StudentViewModel();
            StudentDetail studentDetail = studentManager.GetStudentDetailById(id.Value);
            if (studentDetail == null)
            {
                result = HIQResources.errorMessageUnknownRecord;
                return new JsonResult { Data = result };
            } 
                int deleteResult = studentManager.Delete(id.Value);    
                if (deleteResult == 1)
                {
                    vm.Alert.SetSuccessMessage(HIQResources.messageOperationSuccess);
                    TempData["alert"] = vm.Alert;    
                    result = HIQResources.messageOperationSuccess;
                    return new JsonResult { Data = result };
                }                
                vm.Alert.SetErrorMessage(HIQResources.errorMessageUnableToExecuteOperation);
                TempData["alert"] = vm.Alert;
                result = HIQResources.errorMessageUnableToExecuteOperation;
                return new JsonResult { Data = result };
        }
        catch (DbUpdateException ex)
        {
            Log.AddLogRecord(LogManager.LogType.Warning, LogManager.LogPriority.Low, LogManager.LogCategory.Teacher, ex.Message, ex.StackTrace, base.GetLoggedUser());

            result = HIQResources.errorMessageUnableToDeleteRecord;
            return new JsonResult { Data = result };
        }
        catch (Exception ex)
        {
            Log.AddLogRecord(LogManager.LogType.Error, LogManager.LogPriority.High, LogManager.LogCategory.Inscription, ex.Message, ex.StackTrace, base.GetLoggedUser());

            result = HIQResources.errorMessageExceptionOccurred;
            return new JsonResult { Data = result };
        }
    }

这一切都是由一个用Javascript调用这些方法的按钮触发的:

   $(function () {
            var id;
            var button;

            $(".btnDeleteArgs").on("click", function (e) {
                e.preventDefault();
                button = $(e.target);
                id = button.attr('data-hiq-student-id');               
            });    

            $('#ModalDeleteButton').on("click", function (e) {                  
                var token = $('input[name="__RequestVerificationToken"]').val();
                $.post("/Student/Delete/",
                    {
                        __RequestVerificationToken: token,
                        id: id                            
                    },

                    function (data) {
                        $('#myModal .close').click();
                        var baseurl = '@Url.Action("Index")';    
                        var url = baseurl + "?message=" + data;
                        window.location.href = url;
                    });    
            });

说实话,我不认为Javascript是问题,因为问题始于控制器,如果'条款,它甚至不执行!!!

1 个答案:

答案 0 :(得分:0)

您在评论中说 deleteResult是我要删除的个人资料的ID ...在这种情况下,您的if条件应为

if (deleteResult == id.Value)
{