c# - MVC 4 Dropdownlist Pass value by button? -
i have created dropdownlist using html helper. it's able value , bind dropdown.
how can pass selected dropdown value controller?
my view:
@html.dropdownlist("language", new selectlist(viewbag.langlist, "text", "value")) <input type="button" class="btn" title="filter language" value="filter language" onclick="location.href='@url.action("surv_answer_result", "surv_answer", new { survey_id = model[0].survey_id, language = viewbag.langlist })'" />
my controller language , bind dropdown:
public actionresult surv_getlanguage(int survey_id) { var getlanguagelist = r in db.surv_question_ext_model join s in db.surv_question_model on r.qext_question_id equals s.question_id s.question_survey_id == survey_id group new { r, s } r.qext_language grp select grp.firstordefault(); foreach (var item in getlanguagelist.tolist()) { list<selectlistitem> langresult = new list<selectlistitem>(); foreach (var item2 in getlanguagelist) { selectlistitem temp = new selectlistitem(); temp.text = item2.r.qext_language; temp.value = item2.r.qext_language; langresult.add(temp); } viewbag.langlist = langresult; } return view(viewbag.langlist) ; }
and want pass language controller below:
public actionresult surv_answer_result(int survey_id, string language) { list<answerquestionviewmodel> viewmodel = new list<answerquestionviewmodel>(); surv_getlanguage(survey_id); // whatever want... }
your button in view istype="button"
, have attached onclick
event redirect surv_answer_result
passing original viewbag
property method (which not bind string language
because list<selectlistitem>
.
you need form formmethod.get
@using (html.beginform("surv_getlanguage", "controllername", new { survey_id = model[0].survey_id }, formmethod.get)) { @html.dropdownlist("language", (enumerable<selectlistitem>)viewbag.langlist) <input type="submit" ... /> }
notes:
- the
survey_id
has been added form route value viewbag.langlist
enumerable<selectlistitem>
requireddropdownlist()
helper there no point in overhead of creatingselectlist
(selectlist
enumerable<selectlistitem>
)
Comments
Post a Comment