Answer the question
In order to leave comments, you need to log in
Why is ReactJs data not showing up??
Controller
public class EmployeeController : Controller
{
private IUnitOfWork _dataBase;
private IMapper<Employee, EmployeeViewModel> _employeeMapper;
public EmployeeController(IUnitOfWork dataBase, IMapper<Employee, EmployeeViewModel> employeeMapper)
{
_dataBase = dataBase;
_employeeMapper = employeeMapper;
}
public ActionResult Index()
{
return View();
}
public JsonResult Get()
{
var employees = _dataBase.Employees.GetAll();
var employeesVM = new List<EmployeeViewModel>();
foreach (var employee in employees)
{
var employeeVM = _employeeMapper.Map(employee);
employeesVM.Add(employeeVM);
}
return Json(employeesVM, JsonRequestBehavior.AllowGet);
}
}
class Employee extends React.Component {
constructor(props) {
super(props);
this.state = { employees: [] };
}
render() {
return <div>
<p><b>{this.state.employees.Id}</b></p>
<p>Цена {this.state.employees.Name}</p>
</div>;
}
}
ReactDOM.render(
<Employee getUrl="/Employee/Get" />,
document.getElementById("employeeContent")
);
@{
Layout = "~/Views/Shared/_Layout.cshtml";
}
<html>
<head>
<title>Employee</title>
</head>
<body>
<div id="employeeContent"></div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.3.2/react.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.3.2/react-dom.js"></script>
<script src='@Url.Content("~/Scripts/EmployeeIndex.jsx")'></script>
</body>
</html>
Answer the question
In order to leave comments, you need to log in
And where is your ajax request to get the data? I suspect that passing the url parameter to the component is getUrl="/Employee/Get"
not enough. You passed the Url, and then you need to make the load method, in which the data will be pulled by Ajax. And then, when the component is mounted, call this load method.
componentDidMount() {
this.loadData();
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question