簡體   English   中英

從模型中的列表填充數組

[英]Populate Array from List in Model

我將JavaScript函數的數據存儲在數組中。 現在讓事情變得更容易了,我想知道是否有一種方法可以從Model中的List填充數組

我在后端使用ASP.NET MVC框架。

我知道可能有某種方法,但我無法弄清楚:(

是的,有一種方法:看一下這個例子。 希望對您有所幫助:您可以創建JavaScript文件或將其置於視圖中:創建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);
   }

並在您的控制器中:

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

在您看來:只需調用javascript庫即可。

希望對你有幫助

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM