asp.net mvc - MVC drop down list is not picking up selected item? -
my model contains array of zip code items (ienumerable<selectlistitem>
). contains array of selected zip codes (string[]
).
in html page, want render each selected zip code drop down zip code options. first attempt did not work:
@foreach (var zip in model.zipcodes) { html.dropdownlist( "zipcodes", model.zipcodeoptions ) }
i realized although produce drop downs right "name" attribute, wouldn't know element of zipcodes holds value particular box, , might default first one.
my second attempt surprised me. explicitly set proper selectlistitem's selected property true, , still rendered control nothing selected:
@foreach (var zip in model.zipcodes) { html.dropdownlist( "zipcodes", model.zipcodeoptions.select( x => (x.value == zip) ? new selectlistitem() { value = x.value, text = x.text, selected = true } : x ) ) }
there, it's returning new ienumerable<selectlistitem>
contains original items, unless it's selected item, in case element new selectlistitem it's selected property set true. property not honored @ in final output.
my last attempt try use explicit index on string element wanted use value:
@{int zipcodeindex = 0;} @foreach (var zip in model.zipcodes) { html.dropdownlist( "zipcodes[" + (zipcodeindex++) + "]", model.zipcodeoptions ) }
that doesn't work either, , because name no longer "zipcodes", "zipcodes[x]". received kind of read-only-collection error @ first , had change type of zipcodes property string[]
list<string>
.
in forth attempt, tried following:
@for (int zipcodeindex = 0; zipcodeindex < model.zipcodes.count; zipcodeindex++) { var zip = model.zipcodes[zipcodeindex]; html.dropdownlistfor( x => x.zipcodes[zipcodeindex], model.zipcodeoptions ) }
that produces controls id "zipcodes_1_" , names "zipcodes[1]", not select right values. if explicitly set selected property of right item, works:
@for (int zipcodeindex = 0; zipcodeindex < model.zipcodes.count; zipcodeindex++) { var zip = model.zipcodes[zipcodeindex]; html.dropdownlistfor( x => x.zipcodes[zipcodeindex], model.zipcodeoptions.select( x => (x.value == zip) ? new selectlistitem() { value = x.value, text = x.text, selected = true } : x ) ) }
however, problem approach if add new drop downs in javascript , give them name "zipcodes", override explicitly indexed ones, never make server. doesn't seem mixing plain "zipcodes" name explicit array elements "zipcodes[1]", though map same variable when either used exclusively.
in u.i., user's can click button add new drop down , pick zip code. they're named zipcodes, posted zipcodes array. when rendering fields in loop above, expect read value of property @ given index, doesn't work. i've tried remapping selectlistitems proper option's "selected" property true, still renders control nothing selected. going wrong?
the reason first 2 snippets not work zipcodes
property in model, , value of property determines selected (not setting selected
value in selectlist
constructor ignored). since value of zipcodes
array of values, not single value matches 1 of option values, match not found , therefore first option selected (because has be). note internally, helper method generates new ienumerable<selectlistitem>
based on 1 provided, , sets selected
attribute based on model value.
the reason 3rd , 4th snippets not work, due known limitation of using dropdownlistfor()
method, , make work, need use editortemplate
, pass selectlist
template using additionalviewdata
, or construct new selectlist
in each iteration of loop (as per last attempt). note needs is
for(int = 0; < model.zipcodes.length; i++) { @html.dropdownlistfor(m => m.zipcodes[i], new selectlist(model.zipcodeoptions, "value", "text", model.zipcodes[i])) }
if want use common name (without indexers) each <select>
element using dropdownlist()
method, needs name not match model property, example
foreach(var item in model.zipcodes) { @html.dropdownlist("selectedzipcodes", new selectlist(model.zipcodeoptions, "value", "text", item)) }
and add additional parameter string[] selectedzipcodes
in post method bind values.
alternatively, use for
loop , dropdownlistfor()
method above, include hidden input indexer
allows non-zero based, non consecutive collection items submitted controller , modify script add new items using technique shown in this answer
note example of using editortemplate
additionalviewdata
shown in this answer
Comments
Post a Comment