2

Using entity framework I scaled a model 'beer.cs'

  public class Beer
  {
    [Key]
    public int ID { get; set; }
    public bool HasTasted {get; set; }
    public string Name { get; set; }
  }

I took the automated code from BeersController and switched it to output Json

   public ActionResult Index()
   {
        return View(db.Beers.ToList());
   }

    public ActionResult IndexVM()
    {
        return Json(db.Beers.ToList(), JsonRequestBehavior.AllowGet);
    }

Now the problem is, I'm getting json back without a model name so it looks like this

[{"ID":1,"HasTasted":true,"Name":"Root Beer Ale"},{"ID":2,"HasTasted":false,"Name":"Dragons Breath"},{"ID":3,"HasTasted":false,"Name":"ScAles"},{"ID":4,"HasTasted":true,"Name":"Dragons Breath"}]

Which I believe is the reason it's not rendering on my ng-repeat model.

 <p>{{model | json}}</p>
    <table class="table">
        <tr ng-repeat="beer in model.Beers">

Is there other ways to use ng-repeat or possibly change db.Beers.ToList() to something else that will give me a model name?


BeerCtrl.js

angular.module('AngularDemo.BeerController', [])
.controller('BeerCtrl', [
    '$scope', '$http', function ($scope, $http) {
        $scope.model = {};

        $http.get('/Beers/IndexVM').success(function (data) {
            $scope.model = data;
    });
}]);
7
  • 1
    return Json(new {Beers = db.Beers.ToList()}, JsonRequestBehavior.AllowGet); gives you a name for your json object.. but not sure if that's all you're missing Commented Oct 26, 2015 at 15:40
  • 2
    The collection of Beer objects is the model. I would think you'd just be able to write ng-repeat="beer in model". Commented Oct 26, 2015 at 15:42
  • To make things easier - could you post the Angular code that takes in the JSON data from the controller please? Commented Oct 26, 2015 at 16:06
  • @JasonEvans I added the angular controller to the post Commented Oct 26, 2015 at 16:08
  • 1
    What happens if you change the code to be <tr ng-repeat="beer in model"> Is the data displayed in that case? Commented Oct 26, 2015 at 16:10

1 Answer 1

1

Try

ng-repeat="beer in model"

The property Beers does not exist - the data read from the controller is being set into the model, which acts as an array of objects. Thus you can iterate model itself when accessing the beer data.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.