绑定knockout.js

时间:2015-05-06 13:35:40

标签: javascript jquery model-view-controller knockout.js asp.net-mvc-5

我要求提供员工的教育详细信息和工作经历。所以我决定将Knockout JS用于动态表单。

这是我的代码:

    //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.unshift(new History()) }
        self.removeHistory = function (history) {
            self.historyList.remove(history)
        }
    };

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

    };

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

    //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"));

HTML:

    <div  id="containerHistory" >    
    <div data-bind="foreach: historyList">             
        <div class=" col-md-9 col-lg-10 border ">
         <div ><a href="#" class="hrefRemove" data-bind='click: $parent.removeHistory'>Remove</a></div>          
            <div class="form-group"> 
                @Html.LabelFor(model => model.CompanyName, new { @class = "control-label col-sm-4 col-md-2 col-lg-2" })
                <div class="col-sm-4 col-md-5 col-lg-5">
                    @Html.TextBoxFor(model => model.CompanyName, new { @Class = "form-control", placeholder = "Company Name", data_bind = "value: CompanyName, namePath: true" })
                </div>
                <div class="col-sm-4 col-md-5 col-lg-5">@Html.ValidationMessageFor(model => model.CompanyName) </div>
            </div>

            <div class="form-group">
                @Html.LabelFor(model => model.Designation, new { @class = "control-label col-sm-4 col-md-2 col-lg-2" })
                <div class="col-sm-4 col-md-5 col-lg-5">
                    @Html.TextBoxFor(model => model.Designation, new { @Class = "form-control", placeholder = "Designation", data_bind = "value: Designation, namePath: true" })
                </div>
                <div class="col-sm-4 col-md-5 col-lg-5">@Html.ValidationMessageFor(model => model.Designation) </div>
            </div>
            <div class="form-group">
                @*@Html.HiddenFor(m=>m.DateOfBirth)*@
                @Html.LabelFor(model => model.StartDate, new { @class = "control-label col-sm-4 col-md-2 col-lg-2" })
                <div class="col-sm-4 col-md-5 col-lg-5">

                    @Html.EditorFor(model => model.StartDate, new { placeholder = "MM/DD/YYYY", @Class = "hasDatepicker dateClass", data_bind = "value: StartDate , namePath: true" })          
                </div>
                <div class="col-sm-4 col-md-5 col-lg-5">@Html.ValidationMessageFor(model => model.StartDate) </div>
            </div>
            <div class="form-group">
                @*@Html.HiddenFor(m=>m.DateOfBirth)*@
                @Html.LabelFor(model => model.EndDate, new { @class = "control-label col-sm-4 col-md-2 col-lg-2" })
                <div class="col-sm-4 col-md-5 col-lg-5">
                    @Html.EditorFor(model => model.EndDate, new { placeholder = "mm/dd/yyyy", @Class = "hasDatepicker dateClass", data_bind = "value: EndDate, namePath:true" })

                </div>
                <div class="col-sm-4 col-md-5 col-lg-5">@Html.ValidationMessageFor(model => model.EndDate) </div>
            </div>
        </div>
        <br />
        <br />

    </div>

        </div>




div id="containerEducation">

    <div data-bind="foreach: educationList">
        <div class=" col-md-9 col-lg-10 border ">
            <div><a href="#" class="hrefRemoveEducation" data-bind='click: $parent.removeEducation'>Remove</a></div>

            <div class="form-group">
                @Html.LabelFor(model => model.Degree, new { @class = "control-label col-sm-4 col-md-2 col-lg-2" })
                <div class="col-sm-4 col-md-5 col-lg-5">
                    @Html.TextBoxFor(model => model.Degree, new { @Class = "form-control", placeholder = "Degree Name", data_bind = "value: Degree, namePath: true" })
                </div>
                <div class="col-sm-4 col-md-5 col-lg-5">@Html.ValidationMessageFor(model => model.Degree) </div>
            </div>

            <div class="form-group">
                @Html.LabelFor(model => model.Institute, new { @class = "control-label col-sm-4 col-md-2 col-lg-2" })
                <div class="col-sm-4 col-md-5 col-lg-5">
                    @Html.TextBoxFor(model => model.Institute, new { @Class = "form-control", placeholder = "Institute Name", data_bind = "value: Institute, namePath: true" })
                </div>
                <div class="col-sm-4 col-md-5 col-lg-5">@Html.ValidationMessageFor(model => model.Institute) </div>
            </div>
              <div class="form-group">
                @*@Html.HiddenFor(m=>m.DateOfBirth)*@
                @Html.LabelFor(model => model.Percentage, new { @class = "control-label col-sm-4 col-md-2 col-lg-2" })
                <div class="col-sm-4 col-md-5 col-lg-5">

                    @Html.TextBoxFor(model => model.Percentage, new { @Class = "form-control", placeholder = "Percentage", data_bind = "value: Percentage, namePath: true" })

                </div>
                <div class="col-sm-4 col-md-5 col-lg-5">@Html.ValidationMessageFor(model => model.Percentage) </div>
            </div>
            <div class="form-group">
                @*@Html.HiddenFor(m=>m.DateOfBirth)*@
                @Html.LabelFor(model => model.YearOfPassing, new { @class = "control-label col-sm-4 col-md-2 col-lg-2" })
                <div class="col-sm-4 col-md-5 col-lg-5">
                    @Html.TextBoxFor(model => model.YearOfPassing, new { @Class = "form-control", placeholder = "YearOfPassing", data_bind = "value: YearOfPassing, namePath: true" })

                </div>
                <div class="col-sm-4 col-md-5 col-lg-5">@Html.ValidationMessageFor(model => model.YearOfPassing) </div>
            </div>
        </div>
        <br />
        <br />

    </div>

</div>

当我尝试添加更多div时,我收到如下控制台错误。

  

未捕获错误:无法解析绑定。消息:ReferenceError:
  historyList未定义;绑定值:foreach:historyList

请注意:绑定单个视图模型时,此方法正常。

1 个答案:

答案 0 :(得分:1)

这就是诀窍。

 var viewModelEmploymentHistory = function () {
            // Operations
            var self = this;
            self.historyList = ko.observableArray([new History()]); // Put one line in by default  
            self.addHistory = function () { self.historyList.unshift(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.unshift(new Education()) };
            self.removeEducation = function (education) {
                self.educationList.remove(education)}
        };