Dale Fraser
Dale Fraser

Reputation: 4758

ViewModel replacement for complex Anonymous Object

So im working with this complex anonymous object

        var result = new 
            {    
                percentage = "hide",
                bullets = (string)null,
                item = new [] {
                    new {
                        value = 16,
                        text = "Day",
                        prefix = (string)null,
                        label = (string)null
                    },
                    new {
                        value = 41,
                        text = "Week",
                        prefix = (string)null,
                        label = (string)null
                    },
                    new {
                        value = 366,
                        text = "Month",
                        prefix = (string)null,
                        label = (string)null
                    }
                }
            };

And I want to convert it into a ViewModel and return it as JSON from a rest API.

What I would like to know is

  1. How do I represent this as a model including the array item entries
  2. How do I then add array items to the array once an instance of the model is created
  3. Does the model need a constructor to initialize the array.

Any help or examples you could provide would be great.

Upvotes: 2

Views: 274

Answers (1)

hutchonoid
hutchonoid

Reputation: 33326

Create a class with the structure:

public class Result
{
    public Result()
    { 
        // initialize collection
        Items = new List<Item>();
    }

    public string Percentage { get; set; }
    public string Bullets { get; set; }
    public IList<Item> Items  { get; set; }

}

public class Item
{
   public int Value { get; set; }
   public string Text { get; set; }
   public string Prefix { get; set; }
   public string Label { get; set; }
}

Then change your code to this:

var result = new Result
            {    
                Percentage = "hide",
                Bullets = (string)null,
                Items = {
                    new Item {
                        Value = 16,
                        Text = "Day",
                        Prefix = (string)null,
                        Label = (string)null
                    },
                    new Item {
                        Value = 41,
                        Text = "Week",
                        Prefix = (string)null,
                        Label = (string)null
                    },
                    new Item {
                        Value = 366,
                        Text = "Month",
                        Prefix = (string)null,
                        Label = (string)null
                    }
                }
            };  
  1. Addressed above with the structure.
  2. Add to collection as follows:

    result.Items.Add(new Item { Value = 367, Text = "Month", Prefix = (string)null, Label = (string)null });

  3. I would initialize the collection in the constructor as above.

To return the json from your controller action add the following:

return Json(result, JsonRequestBehavior.AllowGet);

Upvotes: 4

Related Questions