我正在處理ASP.NET MVC 4測試版中的單頁應用程序,它失敗了(我猜)目前的結果是試圖映射我的控制器返回的JSON。Upshot映射問題asp.net mvc4
我的項目與需要一些個人信息,一些開放性的問題+答案和一些李克特問題的答案+的文章(同意,不同意等)
這裏是我的模型的外觀:
// Essay contains personal information, questions and related answers
public class EssayResult {
public EssayResult() {
if(FinishedAt == null)
FinishedAt = DateTime.Now;
PersonInfo = new PersonInfo();
LikertAnswers = new List<LikertAnswer>();
OpenAnswers = new List<OpenAnswer>();
}
public int EssayResultId {get; set;}
public DateTime? FinishedAt {get; set; }
public virtual int PersonInfoId {get; set; }
public virtual PersonInfo PersonInfo {get; set; }
public virtual ICollection<LikertAnswer> LikertAnswers {get; set; }
public virtual ICollection<OpenAnswer> OpenAnswers {get; set; }
}
// Contains some information about the person who's doing the essay
public class PersonInfo {
public int PersonInfoId {get; set;}
[Required]
public int Age {get; set; }
[Required]
public Education Education {
get { return (Education) EducationValue; }
set { EducationValue = (int) value; }
}
[Required]
public Gender Gender {
get { return (Gender) GenderValue; }
set { GenderValue = (int) value; }
}
[Required]
public bool RobotExperience {get; set; }
// Workaround to get enums mapped
public int EducationValue {get; set; }
public int GenderValue {get; set; }
}
public enum Education {
Middelbaar,
Mbo,
Hbo,
Wo
}
public enum Gender {
Man,
Vrouw
}
public class LikertQuestion {
public int LikertQuestionId {get; set; }
[Required]
public string Name {get; set; }
[Required]
public string Question {get; set; }
}
public class LikertAnswer {
// Primary key and Question-relation
public int LikertAnswerId {get; set; }
public virtual int LikertQuestionId {get; set; }
public virtual LikertQuestion LikertQuestion {get; set; }
public virtual int EssayResultId {get; set; }
public virtual EssayResult EssayResult {get; set; }
// Properties
public LikertScale Answer {get; set;}
}
public class OpenQuestion {
public int OpenQuestionId {get; set; }
public string Question {get; set; }
}
我控制器似乎返回正確的JSON:
{
"EssayResultId": 0,
"FinishedAt": "/Date(1338204633030+0200)/",
"LikertQuestionId": 0,
"LikertAnswers": [
{
"Answer": 0,
"EssayResult": null,
"EssayResultId": 0,
"LikertAnswerId": 0,
"LikertQuestion": {
"LikertQuestionId": 1,
"Name": "PU01",
"Question": "Ik denk dat robots nuttig zijn"
}
},
...
],
"OpenAnswers": [
{
"Answer": null,
"EssayResult": null,
"EssayResultId": 0,
"OpenAnswerId": 0,
"OpenQuestion": {
"OpenQuestionId": 1,
"Question": "Wat komt er als eerste in u op bij het horen van het woord 'robot'?"
},
"OpenQuestionId": 0
},
... ],
"PersonInfo": {
"Age": 12,
"Education": 0,
"EducationValue": 0,
"Gender": 0,
"GenderValue": 0,
"PersonInfoId": 0,
"RobotExperience": false
},
"PersonInfoId": 0
}
一些參考文獻沒有設置,因爲這些對象是由控制器創建的,代替鄰f被拉出DB。
Knockout無法將其映射到一個有效的視圖模型,它不會提示任何錯誤,但當我檢查viewmodel變量與螢火蟲它顯示了一大堆upshot函數等,而不是viewmodel和handfull額外的結果功能。
這是我第一次淘汰賽,結果和SPA項目所以我自upshots有點失去了好好嘗試一下拋出任何錯誤,模型是在服務器端/ DB級正確...
如果我需要提供更多細節讓我知道。 :) 我遵循ToDo-items教程和Steve Sanderson的Delivery Tracker示例,所以我想它必須與我的模型/映射有關。
桑德
更新:我已經刪除了FinishedAt的性能,因爲它顯然是在錯誤的格式,但即使這樣也沒有幫助..仍然不知道爲什麼我的視圖模型將不會呈現, json似乎很好..
如果您可以向我們展示您在ko viewmodel中實際看到的內容,或者您如何試圖將其綁定到結果DS,我們可能會幫助您更多 –