Raja
Raja

Reputation: 129

How to assign a value to textbox while dropdown changed

I have a list from dropdown.The dropdown select to get the value from db,after the string value coudn't assigned to the Textbox.

My controller

 [HttpPost]
        public ActionResult GetCountryBasedCharge(Int64 ID)
        {
            OrderBilling objOrderBilling = new OrderBilling();
            OrderBilling objCalculate = new OrderBilling();
            objCalculate = objOrderBilling.AutoCompleteState(ID);
            string chargeid =Convert.ToString(objCalculate.ShippingCharge);
            return Content(chargeid);
        }

My View

@Html.DropDownList("Country", (IEnumerable<SelectListItem>)ViewBag.countrytitles, 
 "-----------Select----------"
 , new { id = "txtBCountry", @class = "selectStyle" })</td><td><label style="color:red" >*</label>
   <input type="text" class="textbox" id="txtChargeAmts" />


<script type="text/javascript" >

    $(document).ready(function () {
      var CountryID;
        $(function () {
            $("#txtBCountry").change(function () {
                CountryID = $("#txtBCountry option:selected").val();
                $.ajax({
                    type: "POST",
                    url: '../Billing/GetCountryBasedCharge',
                    data: { ID: CountryID },
                    success: function (data) {
                        if (data) {
                            alert(data);
       **$('#txtChargeAmts').html(data);**
                        }
                    }
                });
            });
        });

</script>

Upvotes: 0

Views: 1378

Answers (2)

Gaurav Pandey
Gaurav Pandey

Reputation: 2796

Use

$('#txtChargeAmts').val(data); 

in place of

$('#txtChargeAmts').html(data);

Upvotes: 0

Silagy
Silagy

Reputation: 3083

I assuming that your data parameter is getting a value, if that is the case your problem was that you tried to put the return value as html not as a value in the input text.

When you want to assign a value to form input tags you should use the .val() function

Read .val() jquery documentation

Try to change this line:

$('#txtChargeAmts').html(data);

TO

$('#txtChargeAmts').val(data);

Upvotes: 1

Related Questions