1

I am new to react. I am trying to build a CRUD application with React and .NET MVC. I am able to find the code for only getting content from controller, but not for posting.

Below is the code for getting data from controller:

var App = React.createClass({

        getInitialState: function(){
            return{data: ''};
        },

        componentWillMount: function(){
        var xhr = new XMLHttpRequest();
        xhr.open('get', this.props.url, true);
        xhr.onload = function() {
          var response = JSON.parse(xhr.responseText);

          this.setState({ data: response.result });
        }.bind(this);
        xhr.send();
    },

        render: function(){
            return(
                <h1>{this.state.data}</h1>
            );
        }
});

Please provide me the code to send data from react to controller.

My data class:

public partial class EmployeeTable
    {
        public int EmployeeID { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public string Gender { get; set; }
        public string Designation { get; set; }
        public long Salary { get; set; }
        public string City { get; set; }
        public string Country { get; set; }
    }
Praveen Mohan
  • 211
  • 1
  • 6
  • 16

1 Answers1

1

As mentioned in the comments, sent the data using How to make a rest post call from ReactJS code?

Now create an action in your controller

[HttpPost]
public void GetInfo([FromBody]EmployeeTable data){

}
Neville Nazerane
  • 6,622
  • 3
  • 46
  • 79