c# - Can't this be rewritten more concisely using fluent syntax? -


public static class listoft {      public static list<selectlistitem> toselectlist<t>(this list<t> value,                                                         func<t, string> gettext,                                                         func<t, string> getvalue,                                                         string selectedvalue)     {         list<selectlistitem> items = new list<selectlistitem>();          foreach (var item in value)         {             items.add(new selectlistitem             {                 text = gettext(item),                 value = getvalue(item),                 selected = selectedvalue == getvalue(item)              });         }          return items             .orderby(l => l.text)             .tolist();     } } 

sample usage:

[httpget] public actionresult create() {      list<state> states = new list<state>() { new state {name = "michigan", statecode = "mi" },                                              new state {name = "connecticut", statecode = "ct" }};      stateassessmentcreateviewmodel stateassessmentviewmodel = new stateassessmentcreateviewmodel();     stateassessmentviewmodel.states = states.toselectlist(state => state.name, state => state.statecode, "ct");       return view(stateassessmentviewmodel); } 

view:

@html.dropdownlistfor(model => model.statecode, model.states, "-- none -- ", new { htmlattributes = new { @class = "form-control" }) 

i think syntax in view wrong because formcontrol class attribute isn't formatted. note sure why, though.

 <div class="form-group">             <label class="control-label col-md-2" for="statecode">state</label>             <div class="col-md-10">                 <select htmlattributes="{ class = form-control }" id="statecode" name="statecode"><option value="">-- none -- </option>                                         <option selected="selected" value="ct">connecticut</option>                                     <option value="mi">michigan</option>                         </select>                 <span class="field-validation-valid text-danger" data-valmsg-for="canceldate" data-valmsg-replace="true"></span>             </div>         </div> . 

you have project select :

public static list<selectlistitem> toselectlist<t>(this list<t> source,                                                    func<t, string> gettext,                                                    func<t, string> getvalue,                                                    string selectedvalue) {     return source         .select(x => new selectlistitem         {             text = gettext(x),             value = getvalue(x),             selected = selectedvalue == getvalue(x)         })         .tolist(); } 

Comments

Popular posts from this blog

html - Styling progress bar with inline style -

java - Oracle Sql developer error: could not install some modules -

How to use autoclose brackets in Jupyter notebook? -