2012-12-26 46 views
2

当我调用ko.mapping.toJS的子属性的标签,这是一个observableArray,所得到的JS对象被映射为[{},{},{} ]。关于为什么儿童财产的属性没有被映射的任何想法?在QuestionService.saveQuestion功能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); 
}; 
+0

喜安倍晋三。你QuestionService.saveQuestion功能似乎通过调用ko.mapping.toJSON正确序列化您的数组(见我的回答如下)。如果您可以发布调用该函数的客户端JavaScript部分,则可能可以找出问题所在。但实际的功能似乎很好。 –

回答

0

你ko.mapping代码工作正常。由于我看不到整个应用程序,我无法确定是什么原因导致了您的困难。但是,如果我锻炼你的QuestionService.saveQuestion功能,它正确地推的功能范围的“标签”的内容可变进question.Tags观察到的阵列,和你的ko.mapping.toJSON调用正确生成一个JSON格式的字符串可以通过电线传递。

下面是客户端的代码,我跑锻炼你QuestionService.saveQuestion功能:

<script> 
    sv = {}; 
    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(item); 
      }); 
      console.log(ko.mapping.toJSON(question)); 

      $.ajax("/Home/saveQuestion", { 
       data: ko.mapping.toJSON(question), 
       dataType: "json", 
       type: "post", 
       contentType: "application/json", 
       success: sv.Completed 
      }); 
     }; 
     return { 
      saveQuestion: _saveQuestion 
     }; 
    }(); 
    sv.Completed = function (data) {  
     alert(data.message); 
    }; 

    var input = { Tags : ko.observableArray([])}; 
    sv.QuestionService.saveQuestion(input, sv.Completed); 
</script> 

当我运行这段代码,我看到了正确的序列化JSON字符串调试窗口:

{"Tags":[{"Id":1,"Name":"food"},{"Id":2,"Name":"career"},{"Id":3,"Name":"fax"}]} 

我在服务器端看到一个包含三个成员的C#标记对象数组:

enter image description here

因此,您的QuestionService.saveQuestion函数正确使用ko.mapping.toJSON将您的ViewModel的部分序列化为JSON字符串。您遇到的问题必须位于代码的其他部分。

+0

嗨乔,ko.mapping.toJSON不会序列化标签的子属性,但不知何故属性Id和名称是空的或空的。 – Abe

+0

嗨安倍。在我发布的示例代码中,Id和NAme属性正在填充并正确发送到服务器。我通过屏幕快照进行了更新以显示。如果您可以发布更多的JavaScript代码,包括对QuestionService.saveQuestion的调用,我会查看是否可以重现该问题。但现在我不能。谢谢。 –

+0

一个区别似乎是我在您的变量输入中使用了ko.mapping.fromJS。 – Abe