【问题标题】:How to send data from react to Controller in ASP.NET MVC?如何将数据从响应发送到 ASP.NET MVC 中的控制器?
【发布时间】:2018-04-28 03:56:28
【问题描述】:

我是新来的反应。我正在尝试使用 React 和 .NET MVC 构建一个 CRUD 应用程序。我能够找到仅从控制器获取内容的代码,但不能找到用于发布的代码。

下面是从控制器获取数据的代码:

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>
            );
        }
});

请提供代码以将数据从反应发送到控制器。

我的数据类:

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; }
    }

【问题讨论】:

  • 如何获取控制器中传输的数据?
  • 你有一个包含你要传递的数据结构的类吗?
  • @NevilleNazerane 是的。我会在问题中包含课程。

标签: javascript asp.net reactjs model-view-controller


【解决方案1】:

如 cmets 中所述,使用 How to make a rest post call from ReactJS code? 发送数据

现在在你的控制器中创建一个动作

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

}

【讨论】:

    猜你喜欢
    • 2013-09-28
    • 2020-10-21
    • 1970-01-01
    • 1970-01-01
    • 2014-03-07
    • 2022-01-11
    • 1970-01-01
    • 1970-01-01
    • 2012-02-22
    相关资源
    最近更新 更多