Upshot映射问题asp.net mvc4

时间:2012-05-28 12:46:51

标签: json knockout.js asp.net-mvc-4 upshot

我正在使用ASP.NET MVC 4测试版中的单页应用程序,但是当我尝试映射我的控制器返回的JSON时,它失败了(我猜)。

我的项目与一篇文章有​​关,需要一些个人信息,一些开放式问题+答案以及一些李克特问题+答案(同意,不同意等)

以下是我的模型的外观:

// 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
}

某些引用未设置,因为这些对象是由控制器创建的,而不是从数据库中拉出。

Knockout无法将其映射到有效的viewmodel,它不会提示任何错误,但是当我用firebug检查viewmodel-variable时,它会显示大量的upshot-functions等,而不是viewmodel和一大堆额外的结果功能。

这是我的第一个Knockout,Upshot和SPA项目,所以我有点失落,因为结果不会抛出任何错误,并且模型在服务器端/ db级别是正确的...

如果我需要提供更多详情,请告诉我。 :) 我已经关注了ToDo-items教程和Steve Sanderson的Delivery Tracker示例,所以我想它必须与我的模型/映射有关。?

桑德

更新:我已删除了FinishedAt属性,因为它显然格式错误,但即使这样也无济于事..仍然不知道为什么我的viewmodel无法呈现,json看起来很好..

1 个答案:

答案 0 :(得分:0)

Upshot和服务器端不是Moment的ASP.NET MVC 4 RTM的一部分。 所以我会等一下 - 为将来做决定。