vb.net - html.dropdownlistfor selected value not selected -
been stuck on 2 days...hoping guys can help.
using vb.net mvc4 framework.
i have html.dropdownlistfor in view looks this:
@html.dropdownlistfor(function(model) model.title, new selectlist(model.titles, "key", "value"), "-select-", new {.id = "titles", .class = "form-control"})
model.titles dictionary(of string, string) such short list i'd rather pull directly function instead of database. got idea use dictionary list here:
my function looks this, gets pulled model model.titles
public function getagcreptitles() dictionary(of string, string) dim list new dictionary(of string, string) list.add("a/d/sgt", "a/d/sgt") list.add("a/sgt", "a/sgt") list.add("cpl", "cpl") list.add("det", "det") return list end function
everything works great, displays list , saves selected value database. in debug mode, when explore dropdownlistfor in view, can see model.title has retrieve correct value database (eg. "det"), , can see model.titles contains above list function. hard try, can't dropdownlistfor display model.title list.
the select element within html doesn't have selected option, otherwise looks good:
<select name="title" class="form-control"> <option value="">-select-</option> <option value="a/d/sgt">a/d/sgt</option> <option value="a/sgt">a/sgt</option> <option value="cpl">cpl</option> <option value="det">det</option> </select>
i've tried adding model.title value selectedvalue of selectlist in view. i've tried using list of selectlistitems instead of dictionary , sending view via viewbag.mytitles. i've tried using integers key values (0,1,2,3...). nothing works.
i should add have dropdownlistfor in same view works perfectly. when compare them....this 1 pulls list database selectlistitems selectlist (which uses value , text, instead of key , value respectively), value integer starting @ 0 , text string.
really banging head one.
edit: more info. when submit form, modified data uploaded database, , controller sends model data view. okay....in view dropdownlistfor works!!! click refresh, , lose selected value dropdownlistfor (even though model data identical).
here controller:
<allowanonymous()> _ function update() actionresult if websecurity.isauthenticated dim user = db.agcrepprofile.where(function(agcrep) agcrep.email = websecurity.currentusername).tolist if user.count = 1 dim getagencies = db.agencyprofile.where(function(agency) agency.active = true).orderby(function(agency) agency.name).tolist viewbag.myagencies = new selectlist(getagencies, "agcid", "name") user(0).titles = new mylists().getagcreptitles viewdata("success") = "" return view(user(0)) end if end if return redirecttoaction("index", "home") end function <httppost()> _ <validateantiforgerytoken()> _ function update(agcrep agcrep) actionresult if modelstate.isvalid , websecurity.isauthenticated db.entry(agcrep).state = entitystate.modified db.savechanges() dim agencies = db.agencyprofile.where(function(agency) agency.active = true).orderby(function(agency) agency.name).tolist viewbag.myagencies = new selectlist(agencies, "agcid", "name", agcrep.agcid.tostring) agcrep.titles = new mylists().getagcreptitles viewdata("success") = "changes have been saved" else viewdata("success") = "update failed. please try again." end if return view(agcrep) end function
got it! sweet jebus painful.
turns out can't have viewdata or viewbag containing variable same name model variables.
as removed viewbag("title") top of view, model.title worked perfectly.
Comments
Post a Comment