Upshot mapping problems asp.net mvc4

338 Views Asked by At

I'm working on a Single Page Application in ASP.NET MVC 4 beta, which fails (I guess) at the moment upshots tries to map the JSON returned by my controller.

My project works with an essay which requires some personal information, some open questions + answers and a few Likert-questions + answers (agree, don't agree etc)

Here is how my model looks:

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

My controller seems to return proper 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
}

Some references aren't set since those objects were created by the controller, instead of being pulled out of the DB.

Knockout fails to map this into a valid viewmodel, it doesn't prompt any errors but when i inspect the viewmodel-variable with firebug it shows a whole lot of upshot-functions etc, instead of a viewmodel and a handfull of additional upshot functions.

This is my first Knockout, Upshot and SPA project so i'm kinda lost since upshots doens't throw any errors and the model is correct at serverside/db-level...

If I need to supply any more details let me know. :) I've followed the ToDo-items tutorial and Steve Sanderson's Delivery Tracker example, so i guess it has to be something with my model/mapping..?

Sander

Update: I've removed the FinishedAt property since it's apparently in the wrong format, but even that didn't help.. still don't know why my viewmodel won't render, json seems fine..

1

There are 1 best solutions below

0
On

Upshot and the server side are not part of ASP.NET MVC 4 RTM at the Moment. So I would wait a bit - for the future to be decided.