-1
asp.net mvc를 사용하여 Knockoutjs를 구현하려고합니다. 새 항목을 만들고 성공적으로 저장할 수 있지만 변경을 한 후에 기존 레코드를 업데이트하려고 할 때마다 "잡히지 않은 유형 오류 : String이 함수가 아닙니다."오류가 발생했습니다. 용의자는 새로운 레코드에 대해 자동 생성 된 Guid 인 keyID입니다. 어떤 아이디어라도 내가 누락하거나 잘못하고있는 것에 대해 높이 평가 될 것입니다. 다음은 작성 및 편집, 자바 스크립트 파일에 대한 내 컨트롤러 작업입니다.잡히지 않은 유형 오류 : 문자열이 함수가 아닙니다.
[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");
})
}
});
디버거를 사용해 보셨습니까? – Vache
'saveData' 메쏘드에서'current.keyID()'를 없애고 작동하는지 확인하십시오. – Tanner