asp.net - Select from dropdownlist, chosen value create another same value? -
i have create dropdown list, controller:
string tmpuser = system.web.httpcontext.current.user.identity.name; var kdoseprijavlja = (from dllist in dbcontext.userstables                       dllist.username == tmpuser                       select dllist.userid).firstordefault();  var seznam = dbcontext.customertables.where(m => m.userid == kdoseprijavlja).select(m => m.customer); var seznam2 = dbcontext.customtypes.where(m => seznam.contains(m.id_newcustomertype)).select(m => m.customertype); list<string> result = new list<string>();  foreach (var customer in seznam2) {    result.add(customer); }  viewbag.droplist = new selectlist(result); viewbag.destination = parameters.destination;   and in view index have create this:
@html.dropdownlist("destination", ((selectlist)viewbag.droplist).select(t => new selectlistitem() { text = t.text, value = t.text, selected = (t.text == viewbag.destination) }), "--select--", model.search.destination)   now creates this:

so this, when choose value, , click search (post) save value, choose, fine, but!
my problem is, whant have dropdownlist without option "--select--", first value be, first value dropdownlist. have done this, have remove --select-- index:
@html.dropdownlist("destination", ((selectlist)viewbag.droplist).select(t => new selectlistitem() { text = t.text, value = t.text, selected = (t.text == viewbag.destination) }), model.search.destination)   removing --select-- code works, problem doubles chosen value, example if choose "gmtk" , click search this: (it doubles value)

in controller part, can add selected value in viewbag.
look here http://msdn.microsoft.com/en-us/library/dd492553(v=vs.118).aspx
the value want select, must me pass selectedvalue
string selectedvalue = "gmtk"; viewbag.droplist = new selectlist(result,"id","text",selectedvalue);   hope, may you. have nice day. :)
or correct last part of dropdownlist in razor:
 @html.dropdownlist("destination", ((selectlist)viewbag.droplist).select(t => new selectlistitem() { text = t.text, value = t.text, selected = (t.text == viewbag.destination) }), (selectlist)viewbag.droplist)      
Comments
Post a Comment