Click here to Skip to main content
15,894,172 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have table having productId,productName,Price.
I binded this table to display productName in Dropdownlist like this,
C#
public ActionResult Index()
        {
            ViewBag.Products = new SelectList(storeDb.Products, "ProductId", "Name");
            return View();
        }


my question is when i select a specific productName in dropdown list the price should be shown in the text box. Pls help me
This is the code written to retrive price
C#
[HttpPost]
       public ActionResult GetProductDetails(int id)
       {
           var p = storeDb.Products.Where(c => c.ProductId == id).ToList();
           return Json(p);
       }


and this is my ajax script
C#
script type="text/javascript">
    $.ready(function () {
        $("#Products").change(function () {
            //$("#txtPrice").empty();
            $.ajax({
                url:'@Url.Action("GetProductDetails")',
                type:'post',
                dataType:'JSON',
                data: { id: $("#Products").val() },
                success: function (data) {
                
                    $('#txtPrice').text(data.Price);
                },
                error: function () { alert('something bad happened'); }
                
            });
            
            });
        });
  
</script>

but this is not working , please answer me...
Posted
Updated 24-Sep-14 3:52am
v2

1 solution

Try to use after the changes
public ActionResult Index()
        {
ViewBag.Products = new SelectList(storeDb.Products, "ProductId", "Name","Put your selected value here");
 return View();
        }

Replace your value instead of this "Put your selected value here"
In view
@Html.DropDownList("Products", ViewBag.Products as SelectList, "Select Products", new { style = "width:200px" })

something this way

For the dropdown selection see the following code
<script type="text/javascript">
    $(document).ready(function () {
        $("#Products").change(function () {
            var url = "/ControlerName/GetProductDetails";
            var Products = $("#Products").val();
            $.ajax({
                url: url,
                type: 'post',
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                data: { id: Products },
                success: function (data) {
                    debugger;
                    $('#txtPrice').text(data.d);
                    //It's hope so data.d if not putt debugger and see where is the value 
                },
                error: function () { alert('something bad happened'); } 
            }); 
        });
    });

</script>

Change Url controller name as per your given name
and see It's hope so data.d if not putt debugger and see where is the value
 
Share this answer
 
v3
Comments
nrdhakshinar 2-Sep-14 5:47am    
Thanks for sharing, but this is not working. I can able to display product names in dropdown list, but when i select any item the price is not displaying in the text box,
[no name] 2-Sep-14 5:54am    
ok
I am going to update the solution

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