c# - List<Model> with a parameter that should come from the dropdownlist when changed -


i have view of list type , model has selectlist on change should change actionlink parameter.

i have tried using form , posting contents of model controller not work.

this part of view

@foreach (var item in model) {     <tr>         <td>             @html.displayfor(modelitem => item.coursenumber)         </td>         <td>             @html.displayfor(modelitem => item.coursename)         </td>         <td>             @html.displayfor(modelitem => item.coursescopename)         </td>         <td>             @html.displayfor(modelitem => item.avetmiss)         </td>         <td>             @html.dropdownlistfor(modelitem => item.invoiceoptionsid, item.invoiceoptionslist, "--select--", new {@class = "form-control", id = "ddl" + item.coursenumber})         </td>         <td>             @html.actionlink("events","events",new{id= item.coursenumber,pricekey = item.invoiceoptionsid})         </td>     </tr> } 

the pricekey calculated dropdownlist.

you need use javascript/jquery handle client side events. 1 option modify html (assumes for loop - see notes below)

<td>   @html.dropdownlistfor(m => m[i].invoiceoptionsid, model[i].invoiceoptionslist, "--select--", new { @class = "form-control" }) </td> <td>   <a href="#" class="event" data-id="@model[i].coursenumber">events</a> </td> 

then add following script (requires jquery{version}.js)

$(function() {   var url = '@url.action("events")';   $('.event').click(function() {     var id = $(this).data('id'); // items id     var pricekey = $(this).closest('tr').find('select').val(); // value of associated dropdown     location.href = url + '?id=' + id + '&pricekey=' + pricekey; // redirect   }); }); 

note: not clear if code have shown part of form post back, if so, need use for loop (or custom editortemplate) generate form controls. using foreach loop generates duplicate name attributes (without indexers) not bind model when post form.


Comments