2017-07-05 5 views
0

私はreactJSのJSONリストを解析したいと思っています。 は、ここで私はReactJSのJsonオブジェクトを解析する方法

public class ModelEmployee 
{ 
    public List<Employeelist> Employees { get; set; } 
    public int count { get; set; } 
    public int Pagenumber { get; set; } 
} 
public class Employeelist 
{ 
    public int EmployeeID { get; set; } 
    public string FirstName { get; set; } 
    public string LastName { get; set; } 
    public string Phone { get; set; } 
    public int RoleId { get; set; } 
    public string RoleName { get; set; } 
    public List<string> Roles { get; set; } 
} 

を使用していたモデルですそして、私のコントローラは、これは私が

componentWillMount: function(){ 

    var xhr = new XMLHttpRequest(); 
    xhr.open('get', this.props.url, true); 
    xhr.onload = function() { 
     var response = JSON.parse(xhr.responseText); 
     this.setState({ result: response }); 

    }.bind(this); 
    xhr.send(); 
}, 
render: function(){ 
    var rows = []; 
    this.state.result.Employees.forEach(function (item) { 
     rows.push(<EmployeeRow key={item.EmployeeID} item={item }/>); 
}); 

そのが働いていない各値

を抽出するためにReactJSコードにしようとしているものです

public ActionResult Index(int pageNum = 1) 
    { 
     ModelEmployee model = new ModelEmployee(); 
      model = new ModelEmployee(); 
      model.Employees = FetchAllDevices(pageNum, 10); 
      model.count = (int)Math.Ceiling((float)_empService.TotalCount()/_pageSize); 
      model.Pagenumber = pageNum; 
      return Json(model,JsonRequestBehavior.AllowGet);    
    } 

です。しかし、これでsetStateを変更すると、

this.setState({ result: response.Employees }); 

これは正常に動作し、従業員リストを通過できます。しかし、pagenumberとcount変数にはアクセスできません。これを整理するのを手伝ってもらえますか? ありがとうございます。

答えて

1

また、カウントとページ番号も状態に保存しないでください。

this.setState({ 
    employees: response.Employees, 
    count: response.count, 
    pageNumber: response.PageNumber 
}); 

その後は、ページ番号にアクセスし、それぞれthis.state.pageNumberthis.state.countから数えることができます。 XHRのためのあなたのonLoadコールバックで

0

、あなたが完了したレスポンスを確保するために、readyStateのを確認してくださいする必要があります:これは、あなたが完了し、応答を読んでいる保証し

xhr.onload = function() { 
    if (xhr.readyState === xhr.DONE) { 
     if (xhr.status === 200) { // optionally check the status 
      var response = JSON.parse(xhr.responseText); 
      this.setState({ result: response }); 
     } 
    } 

}.bind(this); 

。これを試して、これで問題が解決したかどうかを確認してください

関連する問題