c# - Getting values for a Model property from a Web API call -


currently i'm populating dropdowns options using api call. in model have this:

public list<selectlistitem> getoptions         {                         {                 var options = webapihelper.download<ienumerable<t>>(                 "controller");                 var dropdownoptions = options.select(x => new selectlistitem { text = x.name, value = x.id.value }).tolist();                 return dropdownoptions;             }         } 

and called in couple of places in .cshtml (e.g. see below):

 @html.dropdownlistfor(m => m.someproperty, model.getoptions)  list<selectlistitem> getdropdownoptions()         {             var currentdropdownitems = model.getoptions;             //some other code modify dropdown options.         } 

will web api hit once when call model.getoptions? or called every time since inside property? if latter, work around issue?

edit: thought little more, better have these controller populate values property? have feeling api call gets placed on every call model.getoptions.

you webapihelper.download() called each time access property and, if editing collection, current implementation degrade performance.

while define private field (say) private list<selectlistitem> _options; , in getter, check if null , if so, populate using webapihelper.download() method, , return _options; still not preferred implementation since cannot unit test app.

private list<selectlistitem> _options; public list<selectlistitem> getoptions {     {     if (_options == null)     {       _options = // call service     }     return _options;   } } 

keep view models dumb possible , make property

public list<selectlistitem> options { get; set; } 

and in controller, initialize model , call webapihelper.download() populate collection (and inject webapihelper controller)


Comments

Popular posts from this blog

sublimetext3 - what keyboard shortcut is to comment/uncomment for this script tag in sublime -

java - No use of nillable="0" in SOAP Webservice -

ubuntu - Laravel 5.2 quickstart guide gives Not Found Error -