Wednesday, 15 July 2015

asp.net mvc - MVC 4 - How to get the selected item from a dropdown list -



asp.net mvc - MVC 4 - How to get the selected item from a dropdown list -

i new mvc. have next razor code:

@using (html.beginform()) { @html.validationsummary(true) <fieldset style="margin:5px"> <legend>list bike sale</legend> <div class="editor-label"> @html.labelfor(model => model.bicyclemanfacturer) </div> <div class="editor-field"> @html.dropdownlist("manufacturerlist") </div> .... .... <div class="float-right"> <input type="submit" value="list bike" /> </div> </fieldset> }

"manufacturerlist" list of selectedlistitem stored in viewbag (i didn't want create models dropdown lists). it's build via method:

private void hydratemanufacturerlist() { var manufacturerlist = (from row in db.bicyclemanufacturer.tolist() select new selectlistitem { text = row.description, value = row.bicyclemanufacturerid.tostring() }).tolist(); manufacturerlist.add(new selectlistitem { text = "-- select manufacturer --", value = "0", selected = true }); viewbag.manufacturerlist = manufacturerlist.orderby(row => row.text); }

i have next code gets called when submit done:

[httppost] public actionresult create(bicyclesellerlisting bicyclesellerlisting) { bicyclesellerlisting.listingdate = system.datetimeoffset.now; if (modelstate.isvalid) { db.bicyclesellerlisting.add(bicyclesellerlisting); db.savechanges(); homecoming redirecttoaction("index"); } homecoming view(bicyclesellerlisting); }

what can't figure how selected manufacturer dropdown list when user posts view controller , method executed.

use

public actionresult create(bicyclesellerlisting bicyclesellerlisting, formcollection collection) { ...

you can inputs including drop downwards selected items collection["manufacturerlist"] or similar depending on drop downwards list name.

asp.net-mvc asp.net-mvc-4

No comments:

Post a Comment