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:

  1. the survey_id has been added form route value
  2. viewbag.langlist enumerable<selectlistitem> required dropdownlist() helper there no point in overhead of creating selectlist (selectlist enumerable<selectlistitem>)

Comments

Popular posts from this blog

java - UnknownEntityTypeException: Unable to locate persister (Hibernate 5.0) -

python - ValueError: empty vocabulary; perhaps the documents only contain stop words -

ubuntu - collect2: fatal error: ld terminated with signal 9 [Killed] -