【问题标题】:Binding the C# list to knockout observable array将 C# 列表绑定到敲除 observable 数组
【发布时间】:2015-07-18 01:04:11
【问题描述】:

我的 MVC 模型中有如下对象列表。

   @Html.EditorFor(model => model.LstEmploymentHistory)
   <button id="btnAddHistory" data-bind="click: addHistory">Add one</button>

我需要在加载时将其绑定到 Knockout 可观察数组。

<script type="text/javascript">
    //Data
    var History = function () {
        var self = this;
        self.CompanyName = ko.observable();
        self.Designation = ko.observable();
        self.StartDate = ko.observable();
        self.EndDate = ko.observable()
    };
    var viewModelEmploymentHistory = function () {
        // Operations
        var self = this;
        self.historyList = ko.observableArray([new History()]); // Put one line in by default           
        self.addHistory = function () { self.historyList.push(new History()) }
        self.removeHistory = function (history) {
            self.historyList.remove(history)


        }
        self.educationList = ko.observableArray([new Education()]); // Put one line in by default  
        self.addEducation = function () { self.educationList.push(new Education()) };
        self.removeEducation = function (education) {
            self.educationList.remove(education)

        }
    };
    //Data
    var Education = function () {
        var self = this;
        self.Degree = ko.observable();
        self.YearOfPassing = ko.observable();
        self.Percentage = ko.observable();
        self.Institute = ko.observable()

    };


    //var masterVM = (function () {
    //    this.viewModelEducationalDetails = new viewModelEducationalDetails(),

    //    this.viewModelEmploymentHistory = new viewModelEmploymentHistory();
    //})();

    //ko.applyBindings(masterVM)
    ko.applyBindings(new viewModelEmploymentHistory());

    // ko.applyBindings(new viewModelEducationalDetails(), document.getElementById("containerEducation"));
    //ko.applyBindings(new viewModelEmploymentHistory(), document.getElementById("containerHistory"));     
</script>

任何人都可以帮助我在页面加载时将 C# 列表绑定到可观察数组吗?

【问题讨论】:

  • 这与 C# 有什么关系?
  • 上次我检查 C# 是否是有效的 MVC 语言? @Jamiec
  • @Liam - 让我换个说法;这和 MVC 有什么关系?
  • 感谢利亚姆的评论。我已经实现了动态添加 div 的功能。我可以添加 div。在页面加载时,默认情况下 div 将是以下代码的一个 bcoz self.addHistory = function () { self.historyList.push(new History()) } 我现在需要一个更新或显示 div 的功能。在加载时我将获得项目列表,我需要使用淘汰赛绑定它们。请指导我如何实现这一目标或最好的方法是什么。?

标签: c# arrays knockout.js asp.net-mvc-5


【解决方案1】:

您需要使用 JavaScript 可以理解的格式,例如 JSON.

所以在你的控制器中你需要serialise the model into a JSON string

类似:

Model.JsonString = JsonConvert.SerializeObject(Model.LstEmploymentHistory);

然后您可以在 JavaScript/MVC 中使用它,有几种方法可以做到这一点。最简单的方法是简单地将其写入输入

@Html.HiddenFor(h => h.JsonString);

然后read it in your js:

var Json = document.getElementById('JsonString').value;

然后turn this back into an array:

var array = JSON.Parse(Json);

然后你需要将它绑定到你的 observable 中。你实际上并没有解释这应该如何工作,所以我把这个留给你......

【讨论】:

    【解决方案2】:

    如果您在 Razor 中绑定,则可以使用以下内容:

    var myModel = {
        LstEmploymentHistory: @Html.Raw(Json.Encode(Model.LstEmploymentHistory))
    };
    

    【讨论】:

      猜你喜欢
      • 2015-04-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-05-03
      • 2012-04-11
      • 1970-01-01
      • 1970-01-01
      • 2018-11-24
      相关资源
      最近更新 更多