locked
Populate the textbox by selecting item from dropdown RRS feed

  • Question

  • User2041008840 posted

    I want to populate the textbox after selecting value from dropdown. 
    i have two table (database table)

    table 1 -  id, Name
    table 2 - Id, Amount, Table1ID

    I populate the dropdown with table 1 
    but i cant get value from table after selecting item from table(dropdown item)

    I am doing this in MVC 

    I have code 

    //controller : 
    public ActionResult dd()
            {
                ViewBag.ItemID = new SelectList(db.ItemsLists, "Id", "Items");
                return View();
            }
    
            public JsonResult GetStockDetails(int id)
            {
                List<ItemsList> stockin = new List<ItemsList>();
                stockin = db.ItemsLists.Where(x => x.Id == id).ToList();
                return Json(stockin, JsonRequestBehavior.AllowGet);
    
    
            }
    
    //view dd.cshtml
    <script src="https://code.jquery.com/jquery-2.2.4.min.js" integrity="sha256-BbhdlvQf/xTY9gja0Dq3HiwQF8LaCRTXxZKRutelT44=" crossorigin="anonymous"></script>
    <script type="text/javascript">
        $(document).ready(function () {
            'use strict'
            $("#ItemID").change(function () {
                $.ajax({
                    type: 'GET',
                    url: '@Url.Action("/dd/GetStockDetails/")',
                    datatype: JSON,
                    data: { 'Id': $("#ItemID").val() },
                    success: function (data) {
                        $('#blogTable tbody').empty();
                        $("#txtRate").val(data[0].Id);
                        if (data.length > 0) {
                            $("#txtRate").val(data[0].Id);
    
                        }
                        $.each(data, function (i, item) {
                    //        var rows = "<tr>"
                    //+ "<td>" + item.PostId + "</td>"
                    //+ "<td>" + item.PostTitle + "</td>"
                    //+ "<td>" + item.ShortPostContent + "</td>"
                    //+ "<td>" + item.MetaDescription + "</td>"
                    //+ "</tr>";
                            $('#blogTable tbody').append(rows);
                        });
                    },
                    error: function (data) { }
                });
            });
        });
    </script>
    <h2>dd</h2>
    @Html.DropDownList("ItemID")
    
    <input id="txtRate" type="text" />

    I can i get the amount value  into textbox from table2 after selecting the dropdown(id from table 1)

    Wednesday, October 24, 2018 9:49 AM

Answers

  • User1724605321 posted

    Hi Prathamesh shende ,

    According to your code sample , the url is not correct in your Ajax :

    @Url.Action(string actionName,string controllerName,object routeValues)

    If GetStockDetails action and dd action is in same controller , you just need to use :

     url: '@Url.Action("GetStockDetails")',

    You can use browser's F12 develop tools to trace the request and modify your codes according to the error .

    Best Regards,

    Nan Yu 

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, October 25, 2018 2:53 AM

All replies

  • User1724605321 posted

    Hi Prathamesh shende ,

    According to your code sample , the url is not correct in your Ajax :

    @Url.Action(string actionName,string controllerName,object routeValues)

    If GetStockDetails action and dd action is in same controller , you just need to use :

     url: '@Url.Action("GetStockDetails")',

    You can use browser's F12 develop tools to trace the request and modify your codes according to the error .

    Best Regards,

    Nan Yu 

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, October 25, 2018 2:53 AM
  • User2041008840 posted

    That's Working Now. 

    Thursday, October 25, 2018 6:52 AM