Click here to Skip to main content
15,906,097 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I retrieved the discounted price via action method GetDis and display it in the view.

I want to pass the discounted price value to the other action method . so i can use it in the controller . How can i pass the value for view to the controller.

HTML
<pre>@foreach (var item in Model) {
    <tr data-productid="@item.Id">
        <td>
            @Html.DisplayFor(modelItem => item.ProductName)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.ProductPrice, new { @id = "check" })
        </td>
       
            <td>

               @Html.DropDownList("Status", new List<SelectListItem>
                 {
                    new SelectListItem{ Text="Student Discount", Value = "1" },
                    new SelectListItem{ Text="Loyalty Discount", Value = "0" }
                 }, "Please select discount", new { @class="ddlStatus" })
                
            </td>
       

        <td>
            @Html.ActionLink("Edit", "Edit", new { id=item.Id }) |
            @Html.ActionLink("Details", "Details", new { id=item.Id }) |
            @Html.ActionLink("Delete", "Delete", new { id=item.Id })
        </td>
    </tr>
}

</table>
<div class="clearfix">
    <div class="pull-right">
        <label>Discount: <span id="lblDiscountAmount"></span></label>
    </div>
</div>


@section scripts{
    <script>
        
        $('.ddlStatus').change(function () {
            var val = $(this).val();
            var productId = $(this).parent('td').parent('tr').attr('data-productid')
            ChangeDis(val, productId);
        });
        function ChangeDis(val, productId) {
           
            $.ajax({
                url: '/Products/GetDis',
                data: {
                    discount: val,
                    productId : productId
                },
                success: function (data) {
                   $('#lblDiscountAmount').text(data);

                }
            });
        }
    </script>
}


This discounted price value is appended on the
<label>Discount: <span id="lblDiscountAmount"></span></label>
I want to send it to the another controller in some action method. how can i do it.

What I have tried:

I have displayed the discounted price in the view . and want to pass the value of discouted price to another controller in a action method.
Posted
Updated 21-Aug-17 7:27am

1 solution

If you want to submit the value with your form, then you need to add a hidden field:
<label>Discount: <span id="lblDiscountAmount"></span></label>
<input type="hidden" name="DiscountAmount" id="DiscountAmount" />
JavaScript
success: function (data) {
    $('#lblDiscountAmount').text(data);
    $('#DiscountAmount').val(data);
}

If you want to pass it via an action link, then you'll need to identify those links, and pass the discount amount via the URL:
@Html.ActionLink("Edit", "Edit", new { id = item.Id }, new { @class = "--js-pass-discount" }) |
@Html.ActionLink("Details", "Details", new { id = item.Id }, new { @class = "--js-pass-discount" }) |
@Html.ActionLink("Delete", "Delete", new { id = item.Id })
JavaScript
$(document).on("click", "a.--js-pass-discount", function(e){
    var discountAmount = $('#lblDiscountAmount').text();
    if (!discountAmount) { return; }
    
    var url = this.href;
    var i = url.indexOf('?');
    url = url + (-1 === i ? '?' : '&') + 'discountAmount=' + encodeURIComponent(discountAmount);
    window.location.href = url;

    e.preventDefault();
});
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900