c# - Send Controller data to Reactjs -
i need save employee details database. idea send employee data controller reacjts json format , reactjs should call webapi post method . webapi take care of saving data in sql server.
my web api code:
//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(); }
my controller :
[httppost] public jsonresult createnew(employee model) { return json(model); }
kindly provide suggestion send employee model reactjs. im new reactjs. im trying pass data controller reactjs.
here code
first need create model javascript object same class in .net
eg.
class employee{ //.net class public int employeeid {get; set;} public string employeename {get; set;} } let employee={ //js employeeid:10, employeename:"abc" }
now can pass object param controller , call api updated model
//call controller fetch('https://mywebsite.com/createnew/', { method: 'post', headers: { 'accept': 'application/json', 'content-type': 'application/json', }, body: json.stringify(employee) }) .then(function(resp){ // call api fetch('https://api.com/createnewemployee/', { method: 'post', headers: { 'accept': 'application/json', 'content-type': 'application/json', }, body: json.stringify(resp) }) .then(function(resp){ // check resp.status == 200 }) })
mvc auto-bind model comparing property name.
Comments
Post a Comment