我想通过 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; }
}
我的控制器是
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);
}
这就是我在 ReactJS 代码中尝试提取每个值的方法
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 }/>);
});
它不工作。但是当我用这个改变 setState 时,
this.setState({ result: response.Employees });
它可以正常工作,并且可以通过员工列表。但无法访问 pagenumber 和 count 变量。你能帮我解决这个问题吗?提前致谢。