c# - How to dynamically set a value to a parameter in an MVC ActionLink using Jquery? -


i have index page lists records in table.

now in each of these records have added dropdownlist , want when select record using actionlink on side send not id of record id of selected value dropdownlist well.

could please advise?

<script type="text/javascript">     $(document).ready(function () {         $("#loanid").change(function () {             var selectedloan = $("#loanid option:selected").val();                                           });         });  </script>  @model ienumerable<project.viewmodels.browsetemplates>  @{     viewbag.title = "index"; }      <h2>available templates </h2>  <script src="~/scripts/jquery-3.1.1.js"></script>  <table class="table">     <tr>          <th>             @html.displaynamefor(model => model.templatelanguage.templatetype.templatetypename)         </th>         <th>             @html.displaynamefor(model => model.templatelanguage.language.languagename)         </th>         <th>             @html.displaynamefor(model => model.templatename)         </th>         <th>             @html.displaynamefor(model => model.templatelanguage.templatetype.category.categoryname)         </th>         <th><label>loan</label></th>         <th></th>     </tr>      @foreach (var item in model)     {          <tr>              <td>                 @html.displayfor(modelitem => item.templatelanguage.templatetype.templatetypename)              </td>                     <td>                 @html.displayfor(modelitem => item.templatelanguage.language.languagename)             </td>             <td>                 @html.displayfor(modelitem => item.templatename)             </td>             <td>                 @html.displayfor(modelitem => item.templatelanguage.templatetype.category.categoryname)             </td>             <td>                 @html.dropdownlist("loans", null, "please select",  htmlattributes: new { @class = "form-control", style = "width:200px", @id="loanid" })             </td>              <td>                 <div id="linkdiv">                     @html.actionlink("generate document", "create", "documents", new { templateid = item.id, loanid = item.loanid }, null)                 </div>             </td>         </tr>     }  </table> 

you need use javascript respond client side events.

replace @html.actionlink() code with

<a href="#" data-url="@url.action("create", "documents", new { templateid = item.id })" class="create">generate document</a> 

in addition, generating invalid html (duplicate id attributes), change code generating <select> to

@html.dropdownlist("loans", null, "please select", new { @class = "form-control loan", id="" }) 

you can use additional loan class name style width.

then add script handle .click() event of link , build url based on value of associated <select> element

$('.create').click(function() {     var selectedloan = $(this).closest('tr').find('.loan').val();     var url = $(this).data('url') + '/' + selectedloan;     location.href = url; // redirect }); 

note above assumes have route definition create method (i.e. want generate ../documents/create/1/1 if not, , generating query string values, be

var url = $(this).data('url') + '&loanid=' + selectedloan; 

which generate ../documents/create?templateid=1&loanid=1


Comments

Popular posts from this blog

Is there a better way to structure post methods in Class Based Views -

performance - Why is XCHG reg, reg a 3 micro-op instruction on modern Intel architectures? -

jquery - Responsive Navbar with Sub Navbar -