0

in my application I have my JS file that reads some info filled in the html page. What I want to do now I passing these info to my controller. So in my script I have:

function addNewEmployee() {
    var newEmployeeName = document.getElementById("newEmployeeName").value;
    var newEmployeeSurname = document.getElementById("newEmployeeSurname").value;
    var newEmployeeDateOfBirth = document.getElementById("newEmployeeDateOfBirth").value;
    var newEmployeeRole = document.getElementById("newEmployeeRole").value;
    var newEmployeeEmail = document.getElementById("newEmployeeEmail").value;

    var newEmployee = {
        name : newEmployeeName,
        surname: newEmployeeSurname,
        dateOfBirth: newEmployeeDateOfBirth,
        role: newEmployeeRole,
        email : newEmployeeEmail
    }

    $.post("api/addemployees", {'newEmployee': newEmployee},   function(result) {

    });
}

I would like to know if passing an object in this way is correct and then how can I receive it on my controller side:

[HttpPost]
[Route("api/addemployees")]
    public bool AddEmployee (Object newEmployee)
    {
        return true;
    }

Unfortunately by debugging on the controller side I realized that this way is not correct because nothing arrives. How can I solve this? Thanks in advance.

0

1 Answer 1

1

Just pass the object like

$.post("api/addemployees", JSON.stringify(newEmployee), function(){
})
Sign up to request clarification or add additional context in comments.

2 Comments

and I receive it on the server side as? string?
@Tarta, As Employee

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.