1

Its already asked question. But still i didnt clear. I have to call POST Method of WEBAPI in reactjs. But i have created model in webapi. So i want to know how to pass the model data to post call in reactjs.

Model :

public class EmployeeModels
{
    public int Id { get; set; }
    public string name { get; set; }
    public string mobile { get; set; }
    public string email { get; set; }
    public string dept { get; set; }
    public string erole { get; set; }
}

My WEBAPI Post Method :

   //Insert new Employee
    public IHttpActionResult CreateNewEmployee(EmployeeModels emp)
    {
        using (var ctx = new Employee())
        {
            ctx.tempemp.Add(new tempemp()
            {
                Id = emp.Id,
                name = emp.name,
                mobile = emp.mobile,
                erole = emp.erole,
                dept = emp.dept,
                email = emp.email
            });
            ctx.SaveChanges();
        }
        return Ok();
    }

Now i should want to post Employeemodel from reactjs. Kindly give any suggestions.

Karthikeyan
  • 173
  • 4
  • 18

2 Answers2

4

I have already given answer:

let employee={
    Id:1,
    name:'abc',
    mobile:123456,
    email:'abc@abc.com',
    dept:'IT',
    role:'Developer'
}

fetch('https://mywebsite.com/CreateNewEmployee/', {
  method: 'POST',
  headers: {
    'Accept': 'application/json',
    'Content-Type': 'application/json',
  },
  body: JSON.stringify(employee)
})
.then(function(resp){
    // your response
})
Piyush Dhamecha
  • 1,485
  • 1
  • 13
  • 22
0

You can use axios library.

npm install axios --save

and then:

axios.post('/CreateNewEmployee/', {
    Id:1,
    name:'abc',
    mobile:123456,
    email:'abc@abc.com',
    dept:'IT',
    role:'Developer'
  })
  .then(function (response) {
    console.log(response);
  })
  .catch(function (error) {
    console.log(error);
  });