gpt4 book ai didi

c# - 如何使用angularJs将Json结果放入表格

转载 作者:太空宇宙 更新时间:2023-11-03 14:39:10 25 4
gpt4 key购买 nike

我正在尝试使用angularJS在mvc5中检索整个表。
Json结果中的数据正在显示,但没有显示在我想要的表中。

我正在使用mvc5和angularjs(1.x)从SQL Server检索数据

这是数据访问层代码:

public List<Employee> ShowAllEmployee()
{
List<Employee> prdList = userdb.Employees.ToList();
return prdList;
}


这是HomeController方法:

 public JsonResult AllEmployee()
{
repo = new Employee_DAL.EmpRepo();
var hotList = repo.ShowAllEmployee();
List<Models.Employee> mList = new List<Models.Employee>();
foreach (var hotl in hotList)
{
Models.Employee hotLocal = new Models.Employee();
hotLocal.EmployeeId = hotl.EmployeeId;
hotLocal.FirstName = hotl.FirstName;
hotLocal.LastName = hotl.LastName;
hotLocal.Designation = hotl.Designation;
//hotLocal.Description = hotl.Description;
hotLocal.Country = hotl.Country;
hotLocal.Gender = hotl.Gender;
//hotLocal.Rating = hotl.Rating;
//hotLocal.Email = hotl.Email;
mList.Add(hotLocal);
}
return Json(mList, JsonRequestBehavior.AllowGet);
}


这是脚本(角度)代码:

var myApp = angular.module('MyModule', []);
myApp.controller('DBcontroller', function ($scope, $http) {
$http.get('/Home/AllEmployee') // added an '/' along with deleting Controller portion
.then(function (response) {
$scope.students = response.data
})
})



这是我的观点(cshtml):

@{
Layout = null;
}
@Scripts.Render("~/bundles/jquery")
@Scripts.Render("~/bundles/bootstrap")
<!DOCTYPE html>

<html>
<head>
<meta name="viewport" content="width=device-width" />
<script src="~/Customs/angular.min.js"></script>
<script src="~/Customs/Scripts.js"></script>
<title>AllEmployee</title>
</head>
<body ng-app="myApp">

<div ng-controller="DBcontroller">

<table class="table-bordered">

<tr>

<th>Emp No</th>

<th>First Name</th>

<th>Last Name</th>

<th>Designation</th>

<th>Country</th>

<th>Gender</th>

</tr>

<tr ng-repeat="e in students" @*ng-class-even="'even'" ng-class-odd="'odd'"*@>

<td>{{e.EmployeeId}}</td>

<td>{{e.FirstName}}</td>

<td>{{e.LastName}}</td>

<td>{{e.Designation}}</td>

<td>{{e.Country}}</td>

<td>{{e.Gender}}</td>

</tr>

</table>

</div>



</body>
</html>


我希望将jsonresult显示在表中。在浏览器中并非如此。 Screenshot of my current output

最佳答案

您已经创建了一个名为MyModule的模块:

var myApp = angular.module('MyModule', []);


但在模板中使用:

ng-app="myApp"


将声明更改为:

var myApp = angular.module('myApp', []);


阅读有关模块的更多信息: https://docs.angularjs.org/api/ng/function/angular.module

关于c# - 如何使用angularJs将Json结果放入表格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58301298/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com