observableArray 인 하위 속성 Tags에서 ko.mapping.toJS를 호출하면 결과 JS 객체가 [{}, {}, {} ]; 자식 속성의 속성이 매핑되지 않는 이유에 대한 아이디어가 있습니까?ko.mapping.toJS 하위 관찰 가능 배열 속성에서 작업하지 않습니다.
// Question
sv.QuestionService = function() {
var _saveQuestion = function (question, callback) {
var tags = [
{
Id: 1,
Name: "food"
},
{
Id: 2,
Name: "career"
},
{
Id: 3,
Name: "fax"
}
];
$.each(tags, function (index, item) {
question.Tags.push({ Id: 1, Name: "food" });
});
console.log(question.Tags());
$.ajax("/Interview/saveQuestion", {
data: ko.mapping.toJSON(question),
dataType: "json",
type: "post",
contentType: "application/json",
success: callback
});
};
return {
saveQuestion: _saveQuestion
};
}();
// Question view model
sv.QuestionViewModel = function (data) {
var self = this;
if (!data.QuestionType) {
data.QuestionType = "Not Specified";
}
this.Tags = ko.observableArray([]);
ko.mapping.fromJS(data, {}, this);
this.QuestionStatus = ko.computed(function() {
return this.IsApproved ? "Pending Interview Question" : "Approved Interview Question"
}, this);
this.TagsText(this.TagsText() || "None");
};
// C#
public class InterviewQuestionViewModel {
public long Id { get; set; }
public string Text { get; set; }
public string QuestionType { get; set; }
public long? QuestionTypeId { get; set; }
public string RequestorName { get; set; }
public bool IsAdminApproved { get; set; }
public bool IsActive { get; set; }
public string TagsText { get; set; }
public List<Tag> Tags { get; set; }
public InterviewQuestionViewModel() {
Tags = new List<Tag>();
}
}
public class Tag {
[Description("tag_id")]
public long Id { get; set; }
[Description("tag_name")]
public string Name { get; set; }
public bool IsActive { get; set; }
public Tag() {
IsActive = false;
}
}
// Approved Questions view model
sv.ApprovedQuestionListViewModel = function() {
var self = this;
this.questions = ko.observableArray([]);
this.questionCount = ko.computed(function() {
return this.questions().length;
}, this);
this.load = function() {
sv.QuestionService.getApprovedQuestions(function (data) {
var mapped = ko.utils.arrayMap(data, function (item) {
return new sv.QuestionViewModel(item);
});
self.questions(mapped);
});
}.bind(this);
};
안녕하세요 아베. QuestionService.saveQuestion 함수가 ko.mapping.toJSON 호출을 통해 배열을 올바르게 직렬화하는 것으로 보입니다 (내 대답 참조). 함수를 호출하는 클라이언트 측 javascript 부분을 게시 할 수 있다면 문제가있는 곳을 추적 할 수 있습니다. 그러나 실제 기능은 정상적으로 보입니다. –