gpt4 book ai didi

javascript - 如何在 mvc asp.net 中将表单数据从 reactjs 发布到 Controller

转载 作者:行者123 更新时间:2023-11-29 20:59:28 25 4
gpt4 key购买 nike

我是 React 的新手。我正在尝试在 asp.net 中创建一个带有反应的表单。当将表单数据从 reactjs 发布到 mvc asp.net 中的 Controller 时,数据未被传递。我在下面分享了我的代码。请让我知道我必须进行哪些更改才能使其正常工作。

Controller :

[Route("InputEmployee")]

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

Reactjs 代码:

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'))

值没有被传递:

enter image description here

在追加中进行更改后:

enter image description here

最佳答案

您没有正确构建您的 FormData 对象,append是一个函数,所以你需要这样使用它:

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

此外,我建议使用 native Fetch API用于发送 Web 请求,因为它比处理原始 XHR 请求要简洁得多。它还不是 100% 跨浏览器,但有各种库可以用作 polyfill,例如whatwg-fetch

关于javascript - 如何在 mvc asp.net 中将表单数据从 reactjs 发布到 Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47332258/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com