2

I am new to React. I am trying to create a form in asp.net with react. when posting form data from reactjs to controller in mvc asp.net, the data is not getting passed. I have shared my code below. Please let me know what changes I have to make to make it work.

controller:

[Route("InputEmployee")]

        public void InputEmployee([FromBody]EmployeeTable Employee)
        {
            db.EmployeeTables.Add(Employee);
                   db.SaveChanges();           
        }

Reactjs code:

class InputValues extends React.Component {
    constructor(props) {
        super(props);
        this.handleSubmit = this.handleSubmit.bind(this);
    }

    handleSubmit(e) {
        e.preventDefault();
        const data = new FormData();
        data.append = this.firstName.value;
        data.append = this.lastName.value;
        data.append = this.gender.value;
        data.append = this.designation.value;
        data.append = this.salary.value;
        data.append = this.city.value;
        data.append = this.country.value;
        var xhr = new XMLHttpRequest();
        xhr.open('post', this.props.url, true);
        xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
        xhr.onreadystatechange = function() {
            if(xhr.readyState == XMLHttpRequest.DONE && xhr.status == 200) {
                // Do something on success
            }
        }
        xhr.send(data);
    }

    render() {
        return(
          <div>       
              <form onSubmit={this.handleSubmit}>
          <label htmlFor="FirstName">First Name </label><br/>
          <input id="FirstName" type="text"  placeholder="Enter First Name" ref={(firstName) => this.firstName = firstName} />
          <br/><br/>
          <label htmlFor="LastName">Last Name: </label><br/>
          <input id="LastName" type="text"  placeholder="Enter Last Name"  ref={(lastName) => this.lastName = lastName} />
          <br/><br/>
          <label htmlFor="Gender">Gender: </label><br/>
          <input id="Gender" type="text"  placeholder="Gender"  ref={(gender) => this.gender = gender} />
          <br/><br/>
          <label htmlFor="Designation">Designation: </label><br/>
          <input id="Designation" type="text"  placeholder="Enter Designation" ref={(designation) => this.designation = designation} />
          <br/><br/>
          <label htmlFor="Salary">Salary: </label><br/>
          <input id="Salary" type="number"  placeholder="Enter Salary" ref={(salary) => this.salary = salary} />
          <br/><br/>
          <label htmlFor="City">City: </label><br/>
          <input id="City" type="text"  placeholder="Enter City" ref={(city) => this.city = city} />
          <br/><br/>
          <label htmlFor="Country">Country: </label><br/>
          <input id="Country" type="text"  placeholder="Enter Country" ref={(country) => this.country = country} />
          <p>
            <button type="submit">Submit</button>
          </p>
        </form>
      </div>
    );
          }
};

ReactDOM.render(<InputValues url="api/Employee/InputEmployee" />,
          document.getElementById('grid1'))   

values not getting passed:

enter image description here

after making changes in append:

enter image description here

5
  • Possible duplicate of How to post form data to ASP.NET web api controller in reactjs Commented Nov 16, 2017 at 14:50
  • @MarioNikolaus hi. Thanks a lot for your modified code. Do you know why data is not getting passed? Commented Nov 16, 2017 at 14:58
  • 1
    Don't open new question for the same problem. As James pointed out, code had bug in using append function properly, I changed it and now should be good Commented Nov 16, 2017 at 15:04
  • @MarioNikolaus ok. I tried the modified code. But now the controller function is not getting called at all. I have added the screen shot of the data flow in the controller function on running the code. Commented Nov 16, 2017 at 15:25
  • Give a screenshot of the request in your dev tools, to see exactly what is passed on to backend service Commented Nov 16, 2017 at 15:29

1 Answer 1

2

You're not building your FormData object correctly, append is a function so you need to use it as such:

data.append = ...; // wrong, overwrites the append function
data.append('key', 'value'); // correct, calls the function

Also, I'd recommend using the native Fetch API for sending web requests as it's a lot neater than working with raw XHR requests. It's not 100% cross-browser yet but there are various libraries you can use as a polyfill e.g. whatwg-fetch

Sign up to request clarification or add additional context in comments.

Comments

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.