[RESOLVED]How to use Json Result Value in TextBox

Please I will appreciate any help you can give on this please thank you

I have a product table in the database that has Name and Rate

Now I have a dropdown list that has list of product from database and when I pick a product from the list

I use Ajax to query the database and return the Rate of that product using JSON result

this the code that does that

$(function () {
            $('#Lift').change(function () {
                $.getJSON('/Routine/ProductRate/' + $('#Lift').val(),
                    function (data) {
                        $('#rate').val(data);
                    });
            });
        });

Controller JSON

public JsonResult ProductRate(int id)
        {
            var rate = _productService.GetProductById(id).Rate;
            return Json(rate, JsonRequestBehavior.AllowGet);
        }

My problem now is that I cant get the value that is return to that Rate Textbox and calculate with the quantity to get the total amount

this are the three textbox

                              <div class="form-group">
                                <label class="col-sm-4 control-label">Rate =N= </label>
                                <div class="col-sm-8">
                                    @Html.TextBox("Rate", null, new { @class = "form-control", @readonly = "readonly", id = "rate" })
                                </div>
                            </div>
                            <div class="form-group">
                                <label class="col-sm-4 control-label">Total Amount =N= </label>
                                <div class="col-sm-8">
                                    @Html.TextBoxFor(m => m.Amount, new { @class = "form-control", @readonly = "readonly", id = "amount" })
                                </div>
                            </div>


There is anothe textbox that Takes quantity of prouct

Now this were the calculation is taking place

var rate = $('#rate').val();

$('#product').keyup(function (e) {
            var txtVal = $(this).val() * rate;
            $('#amount').attr("value", txtVal);
            $('#amount').formatCurrency();
        });

the rate text is showing the json data but The calculation script look as if there is no value in Rate TextBox

Please am I missing any thing here Please help thank you

 

Hi 7rippl3m,

In your post ,you mentioned that you textbox is showing  the json data,but you can not get the value in the calculation.

First,I suggest that you can add alert to your calculation function like below:

$('#product').keyup(function (e) {
             alert($('#rate').val());
            var txtVal = $(this).val() * rate;
            $('#amount').attr("value", txtVal);
            $('#amount').formatCurrency();
        });

Second,i suggest that you can press f12 to debug your page, check if it has any error message.

Best Regards,

Kevin Shen.

Leave a Reply