Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

487
Views
Ajax call working only for first row of the table and not working for next rows

In my view I'm displaying a table and in table I strongly typed dropdown list and as you change selected item it calls getPrice(int product_id) function through ajax call and returns price of selected item but it only works for 1st row.

HTML

 <tr class="tr_clone" id="1">
     <td>
        @Html.DropDownListFor(model => model.product_id, ViewBag.ProductList as SelectList, "--select product--", new { @class = "form-control sel"}
     </td>
     <td class="product_price" id="product_price" style="text-align: center; font-size: 16px;">@Html.DisplayFor(model => model.product_price, "", "") </td></tr>

<tr class="tr_clone1" id="2">
     <td>
     @Html.DropDownListFor(model => model.product_id, ViewBag.ProductList as SelectList, "--select product--", new { @class = "form-control sel"})
     </td>
     <td class="product_price" id="product_price1" style="text-align: center; font-size: 16px;">@Html.DisplayFor(model => model.product_price, "", "")</td></tr>

Ajax call

 $(function () {
        $('#product_id').change(function () {
            $.ajax({
                type: "POST",
                url: "/Home/GetPrice",
                datatype: "Json",
                data: { product_id: $('#product_id').val() },
                success: function (data) {
                    document.getElementById('product_price').innerHTML = data;
                    multiply();
                },
                error: function (data = 0) {
                    document.getElementById('product_price').innerText = 0;
                    multiply();
                }
            });
        });
    });
about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

html

   @Html.DropDownListFor(model => model.product_id, ViewBag.ProductList as SelectList, "--select product--", new { @class = "form-control sel", @onchange = "gg(this)" })

ajax call

 <script>
    function gg(x) {
        $.ajax({
            type: "POST",
            url: "/Home/GetPrice // GetPrice is name of actionmathod of controller",
            datatype: "Json",
            data: { product_id: $(x).val() },
            success: function (data) {
                //your code here
            },
            error: function () {
                // your code here
            }
        });
    }
</script>
about 4 years ago · Juan Pablo Isaza Report

0

You need to set 2 different ids for each dropdown.

    @Html.DropDownListFor(model => model.product_id, ViewBag.ProductList as SelectList, "--select product--", new { @id = "ddl_product_id_1", @class = "form-control sel"}

@Html.DropDownListFor(model => model.product_id, ViewBag.ProductList as SelectList, "--select product--", new { @id = "ddl_product_id_2", @class = "form-control sel"})

And write change event of individual that dropdown id.

about 4 years ago · Juan Pablo Isaza Report

0

Since you are selecting 2 different products, you model should have 2 id properties - product1_id and product2_id and 2 price properties - product1_price and product2_price

So fix your view accordingly


 <tr class="tr_clone" id="1">
     <td>
        @Html.DropDownListFor(model => model.product2_id, ViewBag.ProductList as SelectList, "--select product--", new { @class = "form-control sel"}
     </td>
     <td class="product_price" id="product_price" style="text-align: center; font-size: 16px;">@Html.DisplayFor(model => model.product1_price, "", "") </td></tr>

<tr class="tr_clone1" id="2">
     <td>
     @Html.DropDownListFor(model => model.product2_id, ViewBag.ProductList as SelectList, "--select product--", new { @class = "form-control sel"})
     </td>
     <td class="product_price" id="product2_price" style="text-align: center; font-size: 16px;">@Html.DisplayFor(model => model.product2_price, "", "")</td></tr>

and you should 2 separate on change too

$(function () {

        $('#product1_id').change(function () {
            $.ajax({
                type: "POST",
                url: "/Home/GetPrice",
                datatype: "Json",
                data: { product_id: $('#product1_id').val() },
                success: function (data) {
                    document.getElementById('product1_price').innerHTML = data;
                    multiply();
                },
                error: function (data = 0) {
                    document.getElementById('product1_price').innerText = 0;
                    multiply();
                }
            });
        });
   $('#product2_id').change(function () {
            $.ajax({
                type: "POST",
                url: "/Home/GetPrice",
                datatype: "Json",
                data: { product_id: $('#product2_id').val() },
                success: function (data) {
                    document.getElementById('product2_price').innerHTML = data;
                    multiply();
                },
                error: function (data = 0) {
                    document.getElementById('product2_price').innerText = 0;
                    multiply();
                }
            });
        });
    });
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!