Hello, I am trying to Pass a Dropdown item from View to Controller and return value to a textbox. I have this
Code
//Dropdown value.. when an item is selected it goes to the controller to get the value and display it on a textbox
---Stock Items---C#
//My Json code from Controller
public JsonResult GetAmountFromItem(int ItemId)
{
var ac = (from c in cdc.Stock
where c.ID == ItemId
select c).FirstOrDefault();
return Json(ac);
}JavaScript
//I want the value to be displayed on this textbox
$(document).ready(function () {
GetAmountFromItem();
})
$(".ItemId").change(function () {
GetAmountFromItem();
});
var GetAmountFromItem = function () {
$.ajax({
url: '@Url.Action("GetAmountFromItem","Stock")',
type: 'GET',
data: {
ItemId: $('.ItemId').val(),
},
success: function (data) {
var avail = $('.Avail').val();
//I am now stock Here... how do I get it done..
$(data).each(
function (index, item) {
$('.ItemId').append('' + item.item + '')
});
},
error: function () {
}
});
}Thank you for your precious time ... I trully appreciate..

Vishal YelveRank #178 · 13.6K reputationPosted Mar 20, 2023, 10:06 AM EDT
Hi, Try this
$(document).ready(function() { $(".ItemId").change(function () { var itemId= $(".ItemId").val(); GetAmountFromItem(itemId); }); function GetAmountFromItem(itemId) { $.ajax({ async:true, type:'GET', dataType:'JSON', contentType:'application/json; charset=utf-8', url:'@Url.Action("GetAmountFromItem","Stock")', data: {itemId: itemId}, success:function(data){ $(".Avail").val(data); }, error:function() { alert("Error while Processing."); } }); } });Naimish MakwanaRank #324 · 7.7K reputationPosted Mar 20, 2023, 11:26 AM EDT
Hi, Please try below
var GetAmountFromItem = function () { $.ajax({ url: '@Url.Action("GetAmountFromItem","Stock")', type: 'GET', data: { ItemId: $('.ItemId').val(), }, success: function (data) { $('.Avail').val(data.Amount); // Update the value of the Available textbox }, error: function () { } }); }Modify your controller action
GetAmountFromItempublic JsonResult GetAmountFromItem(int ItemId) { var ac = (from c in cdc.Stock where c.ID == ItemId select c).FirstOrDefault(); return Json(new { Amount = ac.Amount }); // Return the Amount property as a JSON object }Thanks
Tuhin PaulRank #7 · 71.9K reputationPosted Mar 20, 2023, 9:39 PM EDT
Few changes I have done on the accepted answer and here is my version:
$(document).ready(function() { $(".ItemId").change(function () { var itemId = $(this).val(); getAmountFromItem(itemId); }); function getAmountFromItem(itemId) { $.get({ url: '@Url.Action("GetAmountFromItem", "Stock")', data: { itemId: itemId }, dataType: 'json', success: function(data) { $(".Avail").val(data); }, error: function() { alert("Error while processing."); } }); } });I have changed the AJAX method from $.ajax to the shorthand method $.get, which simplifies the syntax. Also I have removed the async: true option, since it's the default behavior for AJAX requests in jQuery. Removed the contentType option, since it's not necessary for a GET request.