asp.net - SelectList pulling info from ViewBag: not creating value="..." in generated html -
i'm trying create client-side validated dropdown list in mvc4 application using razor view engine.
the code in view generate dropdown list follows:
@html.dropdownlist("test", new selectlist(viewbag.availablelobsdescriptions, viewbag.availablelobsids), "--none--")
note viewbag.availablelobsdescriptions created in controller using ilist while viewbag.availablelobsids created in controller using ilist.
what expected happen have razor generate dropdown menu descriptions populates inner html of each while ids populates value="...". however, there nothing generated value @ all, resulting in this:
<select id="test" name="test"><option value="">--none--</option> <option>n/a</option> <option>aaaa</option> <option>bbbb</option> <option>cccc</option> <option>dddd</option> <option>eeee</option> <option>ffff</option> </select>
looking @ selectlist documentation, http://msdn.microsoft.com/en-us/library/system.web.mvc.selectlist%28v=vs.108%29.aspx, seems me should work. have idea why values not being generated other placeholder?
thanks!
the constructor using looks following:
selectlist(ienumerable, object)
the second parameter here dictates currently selected object collection in first parameter. not dictate object use list of values.
what should doing using selectlist(ienumerable, string, string)
. allows pass name of property (of each element of ienumerable
) use description , 1 value. can produce this:
controller:
// create ienumerable var lobs = new list<lob>(); // assign parameters objects var exampleitem = new lob(); exampleitem.description = "option text"; exampleitem.value = "myvalue"; // populate list , return view. lobs.add(exampleitem); viewbag.lobs = lobs; return view();
view:
@html.dropdownlist("test", new selectlist(viewbag.lobs, "value", "description"), "--none--")
et voila...
<select id="test" name="test"> <option value="">--none--</option> <option value="myvalue">option text</option> </select>
Comments
Post a Comment