Richa
Richa

Reputation: 3289

Populate Array from List in Model

I have data for the JavaScript function stored in an array. Now to make to things easier I was wondering is there a way I can populate a array from the List in Model

I am using an ASP.NET MVC framework in backend.

I know there might be some way but i cannot figure it out :(

Upvotes: 0

Views: 101

Answers (1)

user3383479
user3383479

Reputation:

Yes there is a way : have a look on this example. I hope it will help you: You can create a JavaScript file or put it your view: You create a Point View Model pointsVM

var urlPath = window.location.pathname;

  $(function () {
      ko.applyBindings(pointsVM);
       pointsVM.loadPoints();
   });

var pointsVM= {
   Points: ko.observableArray([]),

   loadPoints: function () {
    var self = this;
    //Ajax Call Get All Points
    $.ajax({
        type: "GET",
        url: urlPath + '/FillIndex',
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (data) {
            self.Articles(data); //Put the response in ObservableArray
        },
        error: function (err) {
            alert(err.status +" : " + err.statusText);
        }
        });

      }
   };

 function Points(Points) {
   this.field1= ko.observable(Points.field1);
   this.field2= ko.observable(Points.field2);
   this.field3= ko.observable(Points.field3);
   }

And In your controller:

  public JsonResult FillIndex()
    {
        return Json(db.YourDB.ToList(), JsonRequestBehavior.AllowGet);
    }

And in Your View : Just call the javascript Library.

Hope it will help

Upvotes: 1

Related Questions