c# - 使用 AngularJs 从数据库中检索和过滤数据

标签 c# angularjs json asp.net-mvc

我正在努力学习和使用 AngularJs。最后做了一个样本,但无法从数据库中检索数据。我正在尝试过滤产品数据并尝试了以下操作:

ASP.NET MVC Controller :

DemoEntities db = new DemoEntities();
public JsonResult GetProducts()
{
   var result = db.Products.ToList();
   return Json(result, JsonRequestBehavior.AllowGet);
}

ProductClient.js:已更新

var ProductApp = angular.module('ProductApp', []); //Created the module

ProductApp.controller('ProductController', function ($scope, ProductService) { //The controller here
    $scope.Product = null;

    ProductService.GetProducts().then(function (d) { //No parameter passed here
        $scope.Product = d.data; 
    }, function () {
        alert('Failed');
    });
});

ProductApp.factory('ProductService', function ($http) { //The product service
    var factory = {};

    factory.GetProducts = function () {
        return $http.get('/Products/GetProducts'); //The ASP.NET MVC controlled defined
    }
    return factory;
});

Index.cshtml:

@{
    ViewBag.Title = "Tutorial - AngularJs";
}

@section scripts{

    <script src="~/Scripts/angular.js"></script>
    <script src="~/AngularFile/ProductClient.js"></script>
}

<h2>Home</h2>
<div ng-app="ProductApp" class="container">
    <br />
    <br />
    <input type="text" class="input-lg" placeholder="Search Product" ng-model="searchProduct" />
    <br />
    <div ng-controller="ProductController">
        <table class="table">
            <tr>
                <td>{{ Product.ProductName }}</td>
                <td>{{ Product.Details }}</td>
                <td>{{ Product.Price }}</td>
                <td>{{ Product.Stock }}</td>
            </tr>
        </table>
    </div>
</div>

下面是示例项目的屏幕截图,它没有显示任何内容。我是否遗漏了什么并且无法弄清楚?

注意:很抱歉问了这个新手问题并坚持了几个小时。

Sample AngularJs

最佳答案

我真的很傻,发现我错过了 ng-repeat。以下解决了它:

<div ng-controller="ProductController">
   <table class="table" ng-repeat="m in Product">
      <tr>
         <td>{{ m.ProductName }}</td>
         <td>{{ m.Details }}</td>
         <td>{{ m.Price }}</td>
         <td>{{ m.Stock }}</td>
      </tr>
   </table>
</div>

关于c# - 使用 AngularJs 从数据库中检索和过滤数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46559767/

相关文章:

c# - “对象”不包含动态的定义

c# - 我希望绑定(bind)到 WPF 列表中的列表

c# - 正则表达式性能缓慢

javascript - AngularJs 不显示 css

angularjs - 如何在没有 $rootScope 的情况下切换 Angularjs ng-show

javascript - 浏览器客户端中的大型 JSON 对象

c# - WPF:更改模板的背景颜色

angularjs - 为什么 $state.transitionTo 或 $state.go 不显示新的 HTML 部分?

json - Postgres 返回带组的 json

android - Flask REST API 使用 JSONArray 进行响应