0

I have a problem, when I try to delete something, it gives me the following error message:

"Error: Unknown Action"

This is my controller:

[Authorize(Roles = "Admin, Staff")]
        [HttpPost]
        [ValidateAntiForgeryToken]

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 };
                }
                else
                {
                    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 };
            }
        }

This is my Javascript:

 $('#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;
                    });
            });

I would need more specific details on this error, it seems to me that the controller and the javascript is right, so I don't really know what possibly can be.

4
  • What's with those indents? Commented Oct 26, 2017 at 17:11
  • When you debug, I assume the controller action is not being hit? Commented Oct 26, 2017 at 17:48
  • Did your Delete action get hit? I think the parameter Id is null so your controller return this one result = HIQResources.errorMessageUnknownAction; return new JsonResult { Data = result }; Commented Oct 27, 2017 at 8:14
  • It is, but i had a value, I did a break point and it's not being read here: int deleteResult = studentManager.Delete(id.Value); if (deleteResult == 1) { The code is being jumped at this part, eventho the delete result has a value. Commented Oct 27, 2017 at 8:51

1 Answer 1

1

If you're going to call $.post then you need to put the [HttpPost] attribute above the method definition. Otherwise, it just assumes that method is actually a GET (which is why the action is "unknown")

EDIT:

Try changing your $.post to this:

$.ajax({
    type: "POST",
    data: {
        __RequestVerificationToken: token,
        id: id
    },
    success: function(data) {
        $('#myModal .close').click();
        var baseurl = '@Url.Action("Index")';

        var url = baseurl + "?message=" + data;
        window.location.href = url;
    }
});
Sign up to request clarification or add additional context in comments.

1 Comment

I forgot the to insert the [HttpPost] in the code, it has the [HttpPost].

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.