Why is HttpGet requiring parameter when it's marked as optional?

Viewed 44

Basically, I have an Ajax method calling a controller HttpGet method. The controller method has two parameters, one is an enum and the other is an optional parameter which is a class object. However, upon making the request, it says that it requires the aforementioned optional parameter.

I have a HttpGet method as follows

        [HttpGet]
        [Route("/api/getemployee(s)")]
        public ActionResult GetEditOrView(BaseAction action = BaseAction.GET, Employee? employee = null)
        {
            if (action == BaseAction.EDIT)
            {
                employee = employee ?? new Employee();
...

and an Ajax method as follows:

function getEditOrView(action, id = null){
        const employee = current_list.find((value) => value.EmployeeId = id);

        $.ajax({
            type: "GET",
            url: "/api/getemployee(s)",
            data: {action, employee},
            headers: {
                'Accept': 'application/json',
                'Content-Type': 'application/json'
            },
            success: function (result) {
                if (action == @((int)BaseAction.GET)) {
                    current_list = result;
                    BindDataToTable(result);
                    return;
                }
                $('body').html(result);
            },
            fail: function (jqXHR, textStatus) {
                alert("Request failed: " + textStatus);
            }
        });     
    }

When run in Postman, I get

{
    "errors": { \\ Fields of class Employee, reduced for the sake of compactness
        "City": [
            "The City field is required."
        ],
        "Email": [
            "The Email field is required."
        ],
    },
    "type": "https://tools.ietf.org/html/rfc7231#section-6.5.1",
    "title": "One or more validation errors occurred.",
    "status": 400,
    "traceId": "00-561f327b8f50bb9287560b5ec1476d48-e2ec518b9f978d6d-00"
}

and in the browser network console

{"errors":{"":["A non-empty request body is required."]},"type":"https://tools.ietf.org/html/rfc7231#section-6.5.1","title":"One or more validation errors occurred.","status":400,"traceId":"00-e6fd5a759763eac79eb2284b86be08a7-a1ec55fd1e78de10-00"}

Please help.

0 Answers
Related