2

I have a c# object (below) that I'm trying to send to my javascript.

My problem is, that while I can iterate over the items in the list, I can't get to the string-property ('Period').

Referencing the object in JS shows no property at all. After Json-encoding in c#, I can still see the property just before returning it to caller (hovering over the result variable in below function):

[OutputCache(Duration = 0, VaryByParam = "None")]
public JsonResult GetRankingList() {
 Response.ContentType = "text/javascript";
 var user = _userService.GetUserByPrincipal(User);

 // Note, we do this while the user waits as we need to make progress in repeated calls to get the compared ranking list.
 _businessLogicServiceMaintenance.PerformMaintenanceSteps();

 //TODO: Replace with userid (Guid)
 var rankingList = _presenterService.GetRankingListForDisplay(user);

 if (rankingList == null)
  return Json("");

 var result = Json(rankingList);
 return result;
}

How on earth can I get past this? Any comments appreciated!

Yours, Anders, Denmark,

public class RankingListForDisplay : List<RankingListLine>
{
    public string Period { get; set; }
}
4
  • How are you creating the JSON from the list and what exactly is the result you are getting before you return it to the caller? Commented Apr 20, 2010 at 16:42
  • Hi drs, Sorry - didn't see the comment. I've edited the question to show the c# code being called. Commented Apr 26, 2010 at 18:27
  • What is the JSON serializer you are using? Commented Apr 26, 2010 at 18:31
  • I presume we are talking about the Json(RankingList) call ? It's System.Web.Mvc.Controller.Json. Commented Apr 28, 2010 at 12:05

1 Answer 1

1

Thanks for taking your time - I found a solution.

I changed above implementation of RankingListForDisplay to the one below. For some reason json likes it way better ;-)

public class RankingListForDisplay 
{
    public List<RankingListLine> Lines { get; set; }
    public string Period { get; set; }

    public RankingListForDisplay()
    {
        Lines = new List<RankingListLine>();
        Period = "<Unspecified>";
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Yes, I find it's usually best to favor composition over inheritance, and that's especially true when your model is meant for serialization and deserialization.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.