C# 如何显示使用ng repeat显示模型值
我不知道如何在ASP.net MVC中将我的模型与我的角度视图连接起来。我试图显示用户模型中的FirstName和LastName值。 以下是我的看法:C# 如何显示使用ng repeat显示模型值,c#,angularjs,asp.net-mvc,C#,Angularjs,Asp.net Mvc,我不知道如何在ASP.net MVC中将我的模型与我的角度视图连接起来。我试图显示用户模型中的FirstName和LastName值。 以下是我的看法: <table class="table" ng-app="UserApp" style="background: white;" height="1200"> <tbody ng-controller="SearchController"> <tr> <th>First N
<table class="table" ng-app="UserApp" style="background: white;" height="1200">
<tbody ng-controller="SearchController">
<tr>
<th>First Name</th>
<th>Last Name</th>
</tr>
<tr ng-repeat="user in User">
<td></td>
<td>{{ user.FirstName }}</td>
<td>{{ user.LastName }}</td>
</tr>
}
</tbody>
最后,脚本:
var app = angular.module('UserApp', []);
app.controller('SearchController', function ($scope) {
$scope.model = @Html.Raw(Json.Encode(Model));
console.log($scope.model);
console.log($scope.model[0].FirstName);
});
我感到困惑的是,脚本中的console.log正在工作,并正确显示了用户对象和所有值。另外,当我使用console.log@scope.model[0]时,也会正确地提取值,但在角度视图中,我根本无法获取要显示的值。因此,最初在我的视图中,我有:
<table class="table" ng-app="UserApp" style="background: white;" height="1200">
<tbody ng-controller="SearchController">
<tr>
<th>First Name</th>
<th>Last Name</th>
</tr>
<tr ng-repeat="user in model"> //model here instead of User..
<td></td>
<td>{{ user.FirstName }}</td>
<td>{{ user.LastName }}</td>
</tr>
}
名字
姓
//此处为模型,而不是用户。。
{{user.FirstName}
{{user.LastName}
}
我认为它不起作用,但结果是,表格在屏幕上一直向下延伸,我看不到显示的值。哎呀。像这样编码C以返回Json对象
public JsonResult Search()
{
List<User> users;
users = db.Users.ToList();
return JSon(users,JsonRequestBehavior.AllowGet);
}
}))
它可能会帮助你你做了
$scope.model=…
,那么你不应该在你的ng repeat
中迭代model
而不是User
?您从未在您的作用域中声明User
属性这是我最初尝试的,我刚刚意识到表的布局方式,我实际上是以这种方式获取值的,但它不在屏幕上,我看不到它。。谢谢
<table class="table" ng-app="UserApp" style="background: white;" height="1200">
<tbody ng-controller="SearchController">
<tr>
<th>First Name</th>
<th>Last Name</th>
</tr>
<tr ng-repeat="user in model"> //model here instead of User..
<td></td>
<td>{{ user.FirstName }}</td>
<td>{{ user.LastName }}</td>
</tr>
}
public JsonResult Search()
{
List<User> users;
users = db.Users.ToList();
return JSon(users,JsonRequestBehavior.AllowGet);
}
app.controller('SearchController', function ($scope) {
$http({
method: 'GET',
url: '/SearchController/Search'
}).then(function successCallback(response) {
$scope.User= response.data;
});