c# - How to use DropdownList selected Value to filter data -
i have dropdown,a textbox , button,dropddown contains columnnames (name,cpr),to see filtered data user selects value dropdown,enters string , clicks button. doing wrote following code,but unable dropdown selected value,except problem code working perfectly.plz tell me im doing mistake. following controller
public actionresult index(string searchtype,formcollection frm) { list<selectlistitem> obj = new list<selectlistitem>(); obj.add(new selectlistitem { text = "agent name", value = "name", selected = true }); obj.add(new selectlistitem {text = "cpr number", value = "cpr"}); viewbag.cmb = obj; var agents = s in db.agents select s; if (searchtype != null) { viewbag.searchstring = searchtype; string cmbcolumnselection = frm["cmb"]; switch (cmbcolumnselection) { case "name": // table=agent,column=name agents = agents.where(s => s.name.tostring().contains(searchtype)); break; default: agents = agents.where(s => s.cpr.tostring().contains(searchtype)); break; } } return view(agents.tolist()); } `
in view:
@using (html.beginform("index", "agent", formmethod.get)) { <p> @html.textbox("searchtype", viewbag.searchstring string) select filter : @html.dropdownlist("cmb") <input type="submit" value="submit" class="btn btn-primary" /> </p> }
don't mix action input parameters , form collection. have start searchtype
, stick same cmb
, drop form collection altogether:
public actionresult index(string searchtype, string cmb)
that's it, cmb
contain value posted drop down. use now:
string cmbcolumnselection = cmb;
or even
switch (cmb) {...
Comments
Post a Comment