C# 如何将数据从组件传递到控制器?

C# 如何将数据从组件传递到控制器?,c#,angular,typescript,asp.net-core,C#,Angular,Typescript,Asp.net Core,从前端传递一个字符串变量(HTML/.ts到C#controller) 我正在浏览angular.io文档和非官方人士制作的演练。我看了一些视频,但似乎都与我的代码无关。我在Visual Studio 2019中启动了一个新项目(ASP.NET Core web application with Angular)。有.ts组件和.cs控制器。我的HTML设置为接受字符串输入。我尝试过使用HTTP POST请求和ajax请求。我可能用错误的论点做得不正确。我咨询过 还有无数其他不在Stac

从前端传递一个字符串变量(HTML/.ts到C#controller)

我正在浏览angular.io文档和非官方人士制作的演练。我看了一些视频,但似乎都与我的代码无关。我在Visual Studio 2019中启动了一个新项目(ASP.NET Core web application with Angular)。有.ts组件和.cs控制器。我的HTML设置为接受字符串输入。我尝试过使用HTTP POST请求和ajax请求。我可能用错误的论点做得不正确。我咨询过

还有无数其他不在StackOverflow上的人

.html


我希望名称字符串在控制器中可用,我可以打印出来,然后在数据库中使用。

根据官方指南:


现在,
inputValue
将在Keyup事件上存储输入字段中的值。

我使用asp.net core Angular template创建了一个演示。它在Keyup上传递数据

1.home.component.html

<input #box (keyup)="onKey(box.value)">
Your name is: {{name}}
3.SampleData控制器(/api/SampleData)


对不起,这不是问题的答案。为你辩护,你不能真正回答这个问题,因为它有点不连贯。不过,在即兴发挥之前最好不要回答。我为我迅速提出的问题道歉。但是正如您所看到的,我的.ts和.html文件已经有了这个功能。您能提供更多关于您拥有的功能的详细信息吗?提供?例如,您如何将其发送到asp.net核心(您的服务)和asp.net api控制器。这正是我要寻找的。非常感谢。
onKey(value: string)
{
    this.name = value;
}
<input (keyup)="onKey($event)">
onKey(event: any) { 
    console.log(event.target.value); 
    let inputValue = event.target.value;    
  }
<input #box (keyup)="onKey(box.value)">
Your name is: {{name}}
import { Component } from '@angular/core';
import { HttpClient } from '@angular/common/http';

@Component({
  selector: 'app-home',
  templateUrl: './home.component.html',
})
export class HomeComponent {
  name: string;

  constructor(
    private http: HttpClient
  ) { }

  onKey(value: string):void {
    this.name = value;
    const formData: FormData = new FormData();
    formData.append('name', this.name);
    this.http.post('https://localhost:44336/api/SampleData/TestName', formData).subscribe(result => {
      console.log(result);
    }, error => console.error(error));
  }
}
[HttpPost("TestName")]
public JsonResult TestName(string name)
    {
        //your logic
        return Json(name);
    }