javascript - 我的 View 没有被渲染(ASP.NET/Angular)

标签 javascript angularjs json asp.net-mvc

我有一个 Controller 正在发送带有 json 对象的数组。 浏览器中只显示数组的内容,而不是显示 View 。

谢谢。

我的 Controller :

 public JsonResult IndexJson()
    {

        var equipas = db.Equipas.Select(t => new { Nome = t.Nome, Abreviatura = t.Abreviatura, Country = t.Country }).ToList();

        return Json(equipas, JsonRequestBehavior.AllowGet);
    }

我的看法:

@{
ViewBag.Title = "getAll";
Layout = "~/Views/Shared/_Layout.cshtml";
}


<div ng-app="ruyApp" ng-controller="equipasCtrl">

<table class="table">
<tr>
    <th>
        Nome
    </th>
    <th>
        País
    </th>
    <th>
       Abreviatura
    </th>

</tr>

<tr ng-repeat="x in myData">
    <td>
        {{x.Nome}}
    </td>
    <td>
        {{x.Country}}
    </td>
    <td>
        {{x.Abreviatura}}
    </td>


</tr>

</table>

</div>

<script src="~/Scripts/AngularScripts.js"></script>

我的脚本:

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

app.controller('equipasCtrl', function ($scope, $http) {
$http.get('/Equipas/IndexJson').then(function (response) {
    $scope.myData = response.data;
    $scope.statustext = response.statusText;
    $scope.statuscode = response.status;

});
});

浏览器输出:

[{"Nome":"Real Madrid","Abreviatura":"RM","Country":"Espanha"},{"Nome":"Benfica","Abreviatura":"BEN","Country":"Portugal"},{"Nome":"FC Porto","Abreviatura":"FCP","Country":"Portugal"},{"Nome":"Barcelona","Abreviatura":"BAR","Country":"Espanha"},{"Nome":"PSG","Abreviatura":"PSG","Country":"França"},{"Nome":"Charlotte Hornets","Abreviatura":"CHA","Country":"EUA"},{"Nome":"Boston Celtics","Abreviatura":"BOS","Country":"EUA"},{"Nome":"Indiana Pacers","Abreviatura":"IND","Country":"EUA"}]

最佳答案

我认为当你返回除ViewResult之外的其他结果时,ViewEngine不会被触发并且不会返回任何 View 。我会将 json 作为模型返回到 View :例如。 return this.View(jsonEquipas) 或者将Equipas作为属性传递给您传递给 View 的 View 模型。

关于javascript - 我的 View 没有被渲染(ASP.NET/Angular),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43116492/

相关文章:

angularjs - 在 AngularJS 中观察模型变化时如何忽略初始负载?

c# - 将对象转换为 JSON,然后将该 JSON 下载为文本文件 [Asp.net core]

php - 如何在android中显示来自外部数据库(mysql)的视频url?

javascript - 如何使用 underscore.js 从 JSON 文件中获取/过滤所有结果

休假时的 Javascript 拖放操作无法正常工作

javascript - 无法使用 Google map javascript api v3 在 Google map 上设置多个标记

json - 将 JSON 对象数组解析为单个文档

javascript - 如何在谷歌地图中获取降落点纬度和经度

AngularJS:为每个端点创建多个工厂?

javascript - 禁用对所有输入和表单的编辑