[英]How to call a ASP.NET CORE WEB API 3.1 from HTML Submit button
I have a very basic question , How to send the text box values on Submit Click to Action method in ASP .Net Core Web API 3.1,我有一个非常基本的问题,如何在 ASP .Net Core Web API 3.1 中的提交点击操作方法上发送文本框值,
<form method="post" >
<label for="fname">First name:</label>
<input type="text" id="fname" name="firstName"><br><br>
<label for="lname">Last name:</label>
<input type="text" id="lname" name="lastname"><br><br>
<label for="email">Email ID:</label>
<input type="text" id="email" name="email"><br><br>
<input type="submit" value="Submit">
</form>
I have created my Employee Model我已经创建了我的员工模型
public string firstName { get; set; }
public string lastname { get; set; }
public string emailid { get; set; }
I have my Action Method too我也有我的行动方法
[HttpPost]
public IActionResult InsertEmployeeData ([FromBody] Employee employee)
{
var emplFirstName = employee.firstName;
var empLastName = employee.lastname;
return Ok();
}
The available samples in internet are mostly with VIEW as example, but i have a plain html file "Home.html" my wwwroot directory of project, Which need to send value to action method on a click of submit button, is there any way of achieving it without using AJAX Call, View.互联网上的可用示例大多以 VIEW 为例,但我有一个纯 html 文件“Home.html”我的项目 wwwroot 目录,需要单击提交按钮将值发送到操作方法,有什么办法在不使用 AJAX 调用、视图的情况下实现它。
Do Share your Ideas/Knowledge分享您的想法/知识
Thank you谢谢
Set name to form将名称设置为表单
<form method="post" name="FormEmployee" >
<label for="fname">First name:</label>
<input type="text" id="fname" name="firstName"><br><br>
<label for="lname">Last name:</label>
<input type="text" id="lname" name="lastname"><br><br>
<label for="email">Email ID:</label>
<input type="text" id="email" name="email"><br><br>
<input type="submit" value="Submit">
</form>
and add script并添加脚本
document.forms["FormEmployee"].addEventListener("submit", e => {
e.preventDefault();
const form = document.forms["FormEmployee"];
const response = await fetch("api/{controller name}", {
method: "POST",
headers: { "Accept": "application/json", "Content-Type": "application/json" },
body: JSON.stringify({
firstName: form.elements["firstName"].value,
lastname: form.elements["lastname"].value,
email: form.elements["email"].value
})
});
});
As far as I know, the fomrbody will not accept Form-data, it will accept json format or else.据我所知,formrbody 不接受 Form-data,它将接受 json 格式或其他格式。
If you want to post the data from html to webapi, you should use [FromForm]
instead and make sure the html page and the web api is inside the same project.如果要将数据从 html 发布到 webapi,则应改用[FromForm]
并确保 html 页面和 web api 在同一项目中。
Then you should add the right controller/action path in the action attribute in the form tag.然后你应该在表单标签的 action 属性中添加正确的控制器/动作路径。
More details, you could refer to below codes:更多细节,您可以参考以下代码:
<form method="post" action="WeatherForecast/InsertEmployeeData">
<label for="fname">First name:</label>
<input type="text" id="fname" name="firstName"><br><br>
<label for="lname">Last name:</label>
<input type="text" id="lname" name="lastname"><br><br>
<label for="email">Email ID:</label>
<input type="text" id="email" name="email"><br><br>
<input type="submit" value="Submit">
</form>
Api:接口:
[HttpPost("InsertEmployeeData")]
public IActionResult InsertEmployeeData([FromForm] Employee employee)
{
var emplFirstName = employee.firstName;
var empLastName = employee.lastname;
return Ok();
}
Result:结果:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.