Join the Stack Overflow Community
Stack Overflow is a community of 6.5 million programmers, just like you, helping each other.
Join them; it only takes a minute:
Sign up

I an a newbie on angularjs. I passed the model from the controller to the view with the following line

@using Forloop.HtmlHelpers
@model Lga.Cms.Core.Dtos.ContentListDto

In my angular table first few lines, i have the following

<div id="productListing">
                <table class="table-responsive">
                    <thead></thead>
                    <tbody>
                        <tr ng-repeat="product in productlist">

from the @model declaration, how do I assigned the collection in Model to productlist so it will contain the array of DTO? So far, I can see anything in productlist

Thanks for any help.

share|improve this question

You need to convert your model to a javascript object and pass that to your angular controller. You can use JsonConvert.SerializeObject() method to do that.

So in your view, add this code.

@section Scripts{   
  <script src="~/Scripts/YourAngularController.js"></script>    
  <script>
    var m = angular.module("app")
         .value("pageModel",@Html.Raw(Newtonsoft.Json.JsonConvert.SerializeObject(Model)));

  </script>    
}

And in your angular controller which is in YourAngularController.js file,

var app = angular.module("app", []);
var ctrl = function (pageModel) {

    var vm = this;
    vm.productlist= pageModel; //assuming pageModel is a list of products

};
app.controller("ctrl", ctrl);

And in your view you can loop through the productList collection.

<div ng-app="app" ng-controller="ctrl as vm">
  <div id="productListing">
     <table class="table-responsive">
          <tr ng-repeat="product in vm.productlist">
                <td>{{product.Name}}</td>
          </tr>
     </table>
  </div>
</div>
share|improve this answer

You could assign your C# VM to an angular controller scope by using ng-init and serializing your C# VM as a Json using JsonConvert.SerializeObject

<div ng-init="[email protected](this.Model)"></div>
<div id="productListing" ng-controller="your controller">
    <table class="table-responsive">
        <thead></thead>
        <tbody>
            <tr ng-repeat="product in productlist">
                <td>{{product.Name}}</td>
            </tr>
        </tbody>
    </table>
</div>
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.