asp.net mvc 5 - MVC 5 get value of DropDownList within a table row -


i have view table of products can added shopping cart. each row has dropdownlist allowed quantities can ordered along button add cart. populating , displaying properly. know how pass item id in actionlink how can value of downdownlist associated table row of actionlink clicked?

i guessing possibly using jquery fires when actionlink clicked?

i thought of making every row form seems overkill.

is there easy mvc way this?

in prepping more info proper question , went ahead , solved it. thank stephen nudge , info.

i tried putting html.beginform around each <tr> tag in details section. did indeed work me. able unique form info post each individual row. however, when enable jquery datatables submit break. datatables must capturing submit or click somehow. haven't figured out made me try jquery seems better way it.

here how construct table data row:

@foreach (var item in model) {     <tr>         <td>             <img src="@item.getfrontimage()" width="100" />         </td>         <td>             <strong>@html.displayfor(modelitem => item.displayname)</strong>         </td>         <td>             @html.displayfor(modelitem => item.customersku)         </td>         <td>             @html.dropdownlist("orderqty", item.getallowedorderquantities(), htmlattributes: new { @class = "form-control" })         </td>         <td>             <a class="btn btn-default pull-right" data-id="@item.id">add cart</a>         </td>     </tr> } 

this creates select id of orderqty , embedded item id in data-id attribute of link. used jquery capture info , post controller. have test div displaying results in example:

// add cart click $('table .btn').click(function () {     // gather data post     var dataaddtocard = {         id: $(this).data('id'),     // data-id attribute (item id)         quantity: $(this).parent().parent().find('select').val()    // selected value of dropdown in same row button clicked     }      // post data controller     $.ajax({         url: '@url.action("addtocart","shopping")',         type: 'post',         data: json.stringify(dataaddtocard),         contenttype: 'application/json',         success: function (data) { $('#result').html(data.id + ' ' + data.quantity); }     }) }); 

the jquery function receives reference link being clicked can extract item id data-id attribute. can reference dropdown (select) in same row using .parent.parent (gets me <tr> tag) , finding next 'select' tag. pretty obvious lot of you.

this works great purposes. can update other elements data returned post.

thank you

karl


Comments

Popular posts from this blog

android - MPAndroidChart - How to add Annotations or images to the chart -

javascript - Add class to another page attribute using URL id - Jquery -

firefox - Where is 'webgl.osmesalib' parameter? -