c# - US States Drop Down List - need State Name Header to Show -


i'd display state name header on results page. here's gets tricky , maybe i'm going wrong.

i using state abbreviation/id value query users use applicationdbcontext , state value stored in table abbr. (not name). can see below in controller block, trying new query main db context pull state name , store in viewbag.

when select state , enter works should viewbag.nameofstate displays this:

 select [extent1].[id] [id], [extent1].[stateabbr] [stateabbr], [extent1].[statename] [statename] [dbo].[stateorprovinces] [extent1] ([extent1].[stateabbr] = @p__linq__0) or (([extent1].[stateabbr] null) , (@p__linq__0 null))       controller:       public actionresult shopsus(string id)             {                             applicationdbcontext shopdb = new applicationdbcontext();                 var statelist = shopdb.users.where(s => s.state == id).orderby(s => s.city);               //second db query state name header display                 if (statelist != null)                 {                     catalogdb db = new catalogdb();                     var _nameofstate = db.stateorprovinces.where(n => n.stateabbr == id);                     viewbag.nameofstate = _nameofstate;                 }                  return view(statelist.tolist());              }      controller using linq instead:              public actionresult shopsus(string id)             {                             applicationdbcontext shopdb = new applicationdbcontext();                 var statelist = shopdb.users.where(s => s.state == id).orderby(s => s.city);                  if (statelist != null)                 {                     catalogdb db = new catalogdb();                     var name = n in db.stateorprovinces                                        n.stateabbr == id                                        select n.statename;                      viewbag.nameofstate = name;                 }                  return view(statelist.tolist());              } 

the value being passed string , matches id passed in controller (id: "al", "ak", whatever selected. thoughts on i've gone wrong?

thanks,

how displaying viewbag object in view? have keep in mind query returning object, not single property of object. should be:

 var _nameofstate = db.stateorprovinces.where(n => n.stateabbr == id).state; 

where "state" column of full state name.

also, when retrieving single record, should use "single", "singleordefault", or "find" (if param primary key) var _nameofstate = db.stateorprovinces.single(n => n.stateabbr == id).state;


Comments

Popular posts from this blog

php - Submit Form Data without Reloading page -

linux - Rails running on virtual machine in Windows -

php - $params->set Array between square bracket -