c# - ViewModel replacement for complex Anonymous Object -
so im working 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 want convert viewmodel , return json rest api.
what know is
- how represent model including array item entries
- how add array items array once instance of model created
- does model need constructor initialize array.
any or examples provide great.
create class 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 code 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 } } };
- addressed above structure.
add collection follows:
result.items.add(new item { value = 367, text = "month", prefix = (string)null, label = (string)null });
i initialize collection in constructor above.
to return json controller action add following:
return json(result, jsonrequestbehavior.allowget);
Comments
Post a Comment