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

  1. how represent model including array item entries
  2. how add array items array once instance of model created
  3. 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                     }                 }             };   
  1. addressed above structure.
  2. add collection follows:

    result.items.add(new item { value = 367, text = "month", prefix = (string)null, label = (string)null });

  3. i initialize collection in constructor above.

to return json controller action add following:

return json(result, jsonrequestbehavior.allowget); 

Comments

Popular posts from this blog

java - UnknownEntityTypeException: Unable to locate persister (Hibernate 5.0) -

python - ValueError: empty vocabulary; perhaps the documents only contain stop words -

ubuntu - collect2: fatal error: ld terminated with signal 9 [Killed] -