如何使用html表在下拉列表中使用knockout绑定JSON对象

时间:2018-01-30 17:26:41

标签: javascript c#-4.0 knockout.js json.net

Knockout binding problem with getjson

当我更改下拉列值时,json数据会更改但不会加载,并抛出此错误:

  

未捕获错误:您无法多次将绑定应用于同一元素。

如何解决这个问题?

$("#VehicleID").change(function () {
    var categoryid = $("#VehicleTypeID option:Selected").val().trim();
    var subcategoryid = $(this).val();

    var url = "../Consignment/LoadratebydetailId";
    $.getJSON(url, { CategoryID: categoryid, SubCategory: subcategoryid },
        function (classesData) {
            //var koNode = document.getElementById('fareDetailbody');
            ko.cleanNode(classesData);
            //ko.cleanNode($element[0]);
            ko.applyBindings({
                teams: classesData,
            });
        });
        $("#rateChargeDiv").hide();
        $("#rateChargeDiv").show();
});


<div id="rateDiv">
    <div>
        <h4>Selected Detail Changed</h4>
        <table>
            <thead>
                <tr>
                    <th>name</th>
                    <th>cloth</th>
                    <th>color</th>
                    <th>size</th>
                    <th>length</th>
                    <th>total</th>
                </tr>
            </thead>
            <tbody id="fareDetailbody" data-bind="foreach: teams">
                <tr>
                    <td data-bind="text: name"></td>
                    <td data-bind="text: cloth"></td>
                    <td data-bind="text: color"></td>
                    <td data-bind="text: size"></td>
                    <td data-bind="text: length"></td>
                    <td data-bind="text: total"></td>
                </tr>
            </tbody>
        </table>
    </div>
<div id="close">close</div></div>


public JsonResult LoadratebydetailId(string CategoryID, string SubCategory)
{
    if (CategoryID == "" || string.IsNullOrEmpty(CategoryID))
    {
        return Json("failure");
    }
    var vehCategory = this.GetratebydetailId(Convert.ToInt32(CategoryID));
    var StatesData = vehCategory.
        Where(cat => cat.VehicleCategoryID==Convert.ToInt32(SubCategory))
        .Select(m => new
        {
            m.name,
            m.cloth,
            m.color,
            m.size,
            m.length,
            m.total
        });
    return Json(StatesData, JsonRequestBehavior.AllowGet);
}

0 个答案:

没有答案