0

I use an autocomplete to introduce the description of a product and get the ProductID and the Price with a javascript function:

<script type="text/javascript">
  $(document).ready(function () {
    $(function () {
      $("#Description").change(function () {
        $("#ProductID").val(Description_autocomplete_hidden.value);
        $("#Price").load('@Url.Action("GetPrice", "Product")', { id: parseInt($("#ProductID").val()) });
      });
    });
  });
</script>

The ProductID works fine, the action, e.g., “\Product\GetPrice\4” is correctly called but I am unable to assign the product price to the $(“#Price”).

The razor code:

<div class="row">
  <div class="label">@Html.Label("Product")</div>
  <div class="input">@Html.AutoComplete("Description","","Product","_Shared")</div>
</div>
<div id ="ProductID"></div>
<br />

<div class="row">
  <div class="label">@Html.Label("Price")</div>
  <div class="input">@Html.Editor("Price")</div>
</div>

The GetPrice() in the Product controller:

public string GetPrice(int id)
{
  return unitOfWork.ProductRepository.GetByID(id).Pvp1.ToString();
}
4
  • Sorry Diodeus, I had not seen this functionality Commented Dec 17, 2012 at 23:06
  • @Joao, Can't suggest any thing until you post GetPrice method's code. Commented Dec 17, 2012 at 23:11
  • @Joao by the way what would you get if you entered this url \Product\GetPrice\4 in the browser? Commented Dec 17, 2012 at 23:12
  • @Bishnu Paudel, I have edited and added de GetPrice(). When I call it, it returns the correct price, e.g., "10" but when I watch $("#Price").val(), I see: "" Commented Dec 17, 2012 at 23:21

3 Answers 3

2

@Carlos is Right! The .load() function tries to set the inner HTML which doesn't work for a text field. You need to set textBox's value to make it work. Simply replace your line $("#Price").load('@Url.Action("GetPrice", "Product")', { id: parseInt($("#ProductID").val()) }); });

With this:

    $.get('@Url.Action("GetPrice", "Product")', { id: parseInt($("#ProductID").val()) },
 function(result) {
        //set the value here    
        $("#Price").val(result);
    });
Sign up to request clarification or add additional context in comments.

Comments

1

Look here:

http://api.jquery.com/load/

jQuery uses the browser's .innerHTML property to parse the retrieved document and insert it into the current document.

According to what is written there, jQuery load method cannot be used to set value attribute of your input.

Please use this: http://api.jquery.com/jQuery.ajax/

It will give you more options. Also in success callback you can use returned value wherever you want, in your case to assign value to input:

$("#Price").val(data);

Comments

1
$(document).ready(function () {
    $(function () {
      $("#Description").change(function () {
        $("#ProductID").val(Description_autocomplete_hidden.value);
  $.ajax({
                        url: '@Url.Action("GetPrice", "Product")',
                        data: { id: parseInt($("#ProductID").val()) },
                        dataType: 'text',
                        type: 'get',
                        success: function (data) {
                           $("#Price").val(data); // 
                        }
                    });        
      });
    });
  });

try this

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.