c# - ASP.NET Mvc Best Way to get List<SelectListItem> From EntityContext? -


i using entityframework dataaccess view dropdownlist of countries enduser in asp.net mvc webapplication.

it not hard thing achieve have little struggle find looking way.

but first of code:

<td>      @html.dropdownlistfor(x => x.parentid, repos.getparents(@model.parentid) </td> 

somewhere deep in real code:

class dummy     {         public string text { get; set; }         public int value { get; set; }     }      private selectlist _parents;     public selectlist parents     {                 {             if (_parents == null)             {                 var parents = entities.instance.partners.select(x => new dummy() { text = x.name, value = x.id }).orderby(x => x.text).tolist();                 parents.insert(0, new dummy());                  _parents = new selectlist(parents, "value", "text");             }             return _parents;         }     }      public selectlist getparents(int? parentid)     {         if (parentid != 0 && parentid.hasvalue)         {             //setselected         }         return parents;     } 

as can see created ** dummy class copy data! why? because x.id.tostring or equivalent methods dont work in linq entity...

still there sqlfunctions.tostring((double)x.id) isnt perfect solution enaught. @ least until have seen made id 4 string this: "*lotsofspaces*4" , trimming each value considered solution me.

the beautiful code solution this:

new selectlist(entities.instance.partners, "id", "name"); 

beautiful isnt it? :) no not! because selects whole data partners - not id , name column.

so question: can tell me clean , looking way data entityframe in dropdown while valuefield int valuetype?

new selectlist(entities.instance.partners.select(o => new { id = o.id, name = o.name }).tolist(), "id", "name"); 

Comments

Popular posts from this blog

plot - Remove Objects from Legend When You Have Also Used Fit, Matlab -

java - Why does my date parsing return a weird date? -

Need help in packaging app using TideSDK on Windows -