我有一个像这样的动作方法
public async Task<ActionResult> Pc()
{
var v = await context.pcs.Select(w => new Code.pcs
{
id = w.id,
name = w.name,
province = w.province
}).ToListAsync();
var jsonSerialiser = new JavaScriptSerializer();
var json = jsonSerialiser.Serialize(v);
return View(v);
}
我通过像这样的角度http来称呼这个
var app = angular.module('myApp', []);
app.controller('myCtrl', function ($scope, $http) {
$http.get("Pc")
.then(function (response) {
$scope.myWelcome = response.data;
});
});
我的问题是如何读取控制器以角度形式返回的模型?response.data正在倾倒整个视图的HTML。
答案 0 :(得分:0)
将您的控制器的返回类型更改为字符串或 JsonResult
public async string Pc()
{
var v = await context.pcs.Select(w => new Code.pcs
{
id = w.id,
name = w.name,
province = w.province
}).ToListAsync();
var jsonSerialiser = new JavaScriptSerializer();
var json = jsonSerialiser.Serialize(v);
return json;
}
并在您的角度代码中进行修改以读取json,然后根据您的要求将此数据绑定到DOM
var app = angular.module('myApp', []);
app.controller('myCtrl', function ($scope, $http) {
$http.get("Pc")
.then(function (response) {
$scope.myWelcome = response.data;
});
});