1

I'm very new to asp net, I'm trying to return an array like this:

[
    {Option: 'Yes', Total: 10},
    {Option: 'No', Total: 8}
]

I have these two objects:

var op1 = new
{
    Option = "Yes",
    Total = 10
};
var op2 = new
{
    Option = "No",
    Total = 8
};

var ret = ???

return Json(ret, JsonRequestBehavior.AllowGet);

How can I do this?

3
  • 1
    Why don't you declare a class that has members of Option and Total and make a serializer so that this makes more sense? Otherwise that op1 and op2 aren't given a specific type that may be where there are issues here. Commented Oct 27, 2015 at 19:24
  • @JBKing. Because both Total actually are something like ctx.Table.Where(x => x.Item > Number).Count() Commented Oct 27, 2015 at 19:26
  • newtonsoft.com/json/help/html/SerializingCollections.htm would be an example of what I mean here that may help you. Notice how they have "Product" as a type that is defined and makes things much easier here. Commented Oct 27, 2015 at 19:30

2 Answers 2

3

You can try this:

var r= new []{op1,op2};

Check in this msdn page Implicitly-typed Arrays in Object Initializers section.

Sign up to request clarification or add additional context in comments.

Comments

0

Try this:

var ret = [op1,op2];

1 Comment

op1 and op2 received a red underline with this message: ; expected

Your Answer

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