未捕获类型错误:字符串不是函数

时间:2014-09-16 20:31:25

标签: knockout.js

我正在尝试使用asp.net mvc实现Knockoutjs,此时我可以创建新项目并成功保存,但每当我尝试更新现有记录后进行更改我得到“未捕获类型错误:字符串不是功能“错误,我怀疑是guid的keyID,为新记录自动生成。任何想法都会因我错过或做错而受到高度赞赏。以下是我的创建和编辑控制器操作以及javascript文件。

[HttpPost]
//[ValidateAntiForgeryToken]
[ValidateInput(false)]
    public ActionResult Create(EvalTypesDTO evaltypesdto)`enter code here`
    {
        if (ModelState.IsValid)
        {
            evaltypesdto.keyID = Guid.NewGuid();
            _evaltypesManager.SaveEvalTypesInformation(evaltypesdto);
            return Json(evaltypesdto.keyID);
        }

        return View(evaltypesdto);
    }

    [HttpPost]
    //[ValidateAntiForgeryToken]
    [ValidateInput(false)]
    public ActionResult Edit(EvalTypesDTO evaltypesdto)
    {
        if (ModelState.IsValid)
        {
            if (evaltypesdto == null)
            {
                return HttpNotFound();
            }

            _evaltypesManager.UpdateEvalTypesInformation(evaltypesdto.keyID, evaltypesdto);
            return Json(evaltypesdto.keyID);
        }
        return View(evaltypesdto);
    }


viewModel = {
myCollection: : ko.observableArray()
};

//retrieve data from the hidden field instead of ajax call to the server
$(document).ready(function () {
var data = JSON.parse($("#serverJSON").val()); //Convert the JSON string to JS object
$(data).each(function (index, element) {
    var mappedItem =
    {
        keyID: (element.keyID),
        DEPT_CD: (element.DEPT_CD),
        blnTemplate: ko.observable(element.blnTemplate),
        strEval: ko.observable(element.strEval),
        strEvalLink: ko.observable(element.strEvalLink),
        intDefaultRequiredToComplete: ko.observable(element.intDefaultRequiredToComplete),
        Mode: ko.observable("display")

    };
    viewModel.myCollection: .push(mappedItem);
});
ko.applyBindings(viewModel);

$(document).on("click", ".kout-edit", null, function (ev) {
    var current = ko.dataFor(this);
    current.Mode("edit");
});

$(document).on("click", ".kout-cancel", null, function (ev) {
    var current = ko.dataFor(this);
    current.Mode("display");
});

$(document).on("click", ".kout-cancelCreate", null, function (ev) {
    var current = ko.dataFor(this);
    viewModel.myCollection: .remove(current)
    current.Mode("display");
});

$(document).on("click", ".kout-update", null, function (ev) {
    var current = ko.dataFor(this);
    saveData(current);
    current.Mode("display");
});

$(document).on("click", ".kout-save", null, function (ev) {
    var current = ko.dataFor(this);
    saveData(current);
    current.Mode("display");
});

$(document).on("click", "#create", null, function (ev) {
    var current = {
        keyID: ko.observable(),
        DEPT_CD: ko.observable(),
        blnTemplate: ko.observable(),
        strEval: ko.observable(),
        strEvalLink: ko.observable(),
        intDefaultRequiredToComplete: ko.observable(),
        Mode: ko.observable("newtemp")
    }

    viewModel.myCollection: .push(current);
});

function saveData(current) {
    var postUrl = "";
    var submitData = {
        keyID: current.keyID(),
        DEPT_CD: ("1251"),
        blnTemplate: current.blnTemplate(),
        strEval: current.strEval(),
        strEvalLink: current.strEvalLink(),
        intDefaultRequiredToComplete: current.intDefaultRequiredToComplete(),
        __RequestVerificationToken: $("input[name='__RequestVerificationToken']").val()
    };
    //console.log(current.keyID());
    if (current.keyID() == null && current.strEval() != "") {
        postUrl = "/EvaluationsManager1/Evaltypes/Create"
    }
    else {
        postUrl = "/EvaluationsManager1/Evaltypes/Edit"
    }
    //Perform Ajax save using jQuery
    $.ajax({
        type: "POST",
        url: postUrl,
        data: JSON.stringify(submitData),
        contentType: "application/json; charset=utf-8",
        dataType: "json",

    }).done(function (id) {
        current.keyID(id);
    }).error(function (ex) {
        alert("ERROR Saving");
    })
}

});

1 个答案:

答案 0 :(得分:0)

I solved it.

Create new:    var submitData = { keyID: current.keyID(), rest of the code
===========
Update:   var submitData = {keyID: current.keyID, rest of the code     
======

Thanks all.