【问题标题】:My view is not being rendered (ASP. NET / Angular)我的视图没有被渲染(ASP.NET / Angular)
【发布时间】:2017-08-24 07:08:37
【问题描述】:

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

谢谢。

我的控制器:

 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"}]

【问题讨论】:

  • 看起来应该可以了。需要解析 JSON 什么的吗?
  • 看起来您的 NET 服务器配置不正确,而是 Angular 路由器您的请求由服务器处理并为您带来 json 数据

标签: javascript angularjs json asp.net-mvc


【解决方案1】:

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

【讨论】:

    【解决方案2】:

    我不知道为什么该代码不起作用。我通过将 ng-init 指令与模型的解析一起解决了这个问题。

    查看

    <div ng-app="ruyApp" ng-controller="equipasCtrl" ng-init="init(@Newtonsoft.Json.JsonConvert.SerializeObject(Model))">
    
    </div>
    

    脚本

    app.controller('equipasCtrl', function ($scope) {
    
    $scope.init = function (equipas) {
    $scope.myData = equipas;
    
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-01-17
      • 1970-01-01
      • 1970-01-01
      • 2011-06-19
      相关资源
      最近更新 更多