populate dropdown list ,on event change of textbox

Analyst_SQL 3,576 Reputation points
2023-03-16T12:08:32.29+00:00

I have below table.i want, when i input in textbox Prdno (10001) then relevent data get display in dropdown list(ItemMasterFile) and label(Orderno)

Create table #itemMasterFile (Codeitem int,Descriptionitem varchar(50))
Create table #Probale (Prdno int,Codeitem int,Orderno int)

Insert into #itemMasterFile values (1,'AAAA')
Insert into #itemMasterFile values (2,'BBBB')

insert into #Probale values(10001,1,22)
insert into #Probale values(10002,2,33)

I tried, but it is not working

 public class OrderVm
    {

        [Display(Name = "Select Order")]
        public int OrderNo { get; set; }
        public int Codeitem { get; set; }
        [Display(Name = "Input Barcode")]
        public int Prdno { get; set; }

    }

 public ActionResult Index()
        {
    
            ViewBag.orderId = new SelectList(DB.SalesOrders.Where(bt => bt.OrderNo > orderId && bt.Status == "Open"), "Orderno", "Order_Ref_No", "0");
       

            var codeitemId = 0;
            ViewBag.codeitemId = new SelectList(DB.ItemMasterFiles.Where(bt => bt.CodeItem > codeitemId && bt.TiD == 1), "CodeItem", "Descriptionitem", "0");
            return View();
        }

 [HttpGet]
        public JsonResult GetBarcodeSB(int prdno)
        {
            OrderVm vm = new OrderVm();
            vm.Prdno = prdno;
            vm.Prdno = DB.Probales.Where(x => x.Prdno == prdno)
                 .Select(x => x.Prdno).FirstOrDefault();
                   
                return Json(vm, JsonRequestBehavior.AllowGet);
           
        }

div>

    <div class="row">
        <div style="display: inline-block;">
            <div>
                @Html.LabelFor(model => model.Prdno, "Barcode", htmlAttributes: new { @class = "control-label col-md-4" })

                @Html.EditorFor(model => model.Prdno, new { htmlAttributes = new { @class = "form-control col-md-10", @id = "Search_Prdno" } })
                @Html.ValidationMessageFor(model => model.Prdno, "", new { @class = "text-danger" })
            </div>
        </div>
        <div style="display: inline-block;">
            <div>
                @Html.LabelFor(model => model.Codeitem, "Select Item", htmlAttributes: new { @class = "control-label col-md-3" })

                @Html.DropDownList("codeitemId", (SelectList)ViewBag.Codeitem, "Select", htmlAttributes: new { @class = "form-control", @id = "select2-3" })

                @Html.ValidationMessageFor(model => model.Codeitem, "", new { @class = "text-danger" })
            </div>
        </div>
    </div>


<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.9/angular.min.js"></script>
<script type="text/javascript">
    var app = angular.module('MyApp', [])
    app.controller('OrderPack', function ($scope, $http, $window) {
        $scope.SetDropDownlist = function () {
            var country = $('#Search_Prdno').val();
            var data = { "select2-3": prdno };
            var config = {
                params: data,
                headers: { 'Accept': 'application/json' }
            };
            $http.get("/OrderPack/GetBarcodeSB", config).then(function (response) {
                $scope.getBarcodeSB = response.data;
            }, function (response) {
                alert(response.responseText);
            });
        }
    });
</script>
Developer technologies ASP.NET Other
Developer technologies C#
0 comments No comments
{count} votes

Accepted answer
  1. Lan Huang-MSFT 30,186 Reputation points Microsoft External Staff
    2023-03-17T07:24:21.4133333+00:00

    Hi @akhter hussain,

    I remember you asking the same question, but with a different database.

    You asked a lot of questions, but in fact some questions are very similar, I think you should learn to infer other cases from one instance.

    <div>
    
        <div class="row">
            <div style="display: inline-block;">
                <div>
                    @Html.LabelFor(model => model.Prdno, "Barcode", htmlAttributes: new { @class = "control-label col-md-4" })
    
                    @Html.EditorFor(model => model.Prdno, new { htmlAttributes = new { @class = "form-control col-md-10", @id = "Search_Prdno" } })
                    @Html.ValidationMessageFor(model => model.Prdno, "", new { @class = "text-danger" })
                </div>
            </div>
    
            <div style="display: inline-block;">
                <div>
                    @Html.LabelFor(model => model.Codeitem, "Select Item", htmlAttributes: new { @class = "control-label col-md-3" })
    
                    @Html.DropDownList("codeitemId", (SelectList)ViewBag.Codeitem, "Select", htmlAttributes: new { @class = "form-control", @id = "select2-3" })
    
                    @Html.ValidationMessageFor(model => model.Codeitem, "", new { @class = "text-danger" })
                </div>
            </div>
           
        </div>
    </div>
    <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
    <script type="text/javascript">
        $(document).ready(function () {
            $("#Search_Prdno").change(function () {
                var prdno = $("#Search_Prdno").val();
                if (prdno != "") {
                    GetSections(prdno);           
                }
                else {
                    $("#select2-3").empty();
                }
            });
        });
        function GetSections(prdno) {
            $.ajax({
                async: true,
                type: 'GET',
                dataType: 'JSON',
                contentType: 'application/json; charset=utf-8',
                url: '/Home/GetBarcodeSB',
                data: { prdno: prdno },
                success: function (data) {
                    $('#select2-3').empty();
                    $(data).each(function (index, item) {
                        $('#select2-3').append($('<option/>', { value: item.Value, text: item.Text }))
                    })
                },
                error: function () {
                    alert("There is some problem to get.");
                }
            });
        }
    </script>
    
    public ActionResult Index()
            {
                var orderId = 0;
                ViewBag.orderId = new SelectList(DB.SalesOrder.Where(bt => bt.OrderNo > orderId ), "Orderno", "OrderTime", "0");
    
    
                var codeitemId = 0;
                ViewBag.codeitemId = new SelectList(DB.ItemMasterFile.Where(bt => bt.CodeItem > codeitemId ), "CodeItem", "Descriptionitem", "0");
                return View();
               
            }
            [HttpGet]
            public JsonResult GetBarcodeSB(int prdno)
            {
    
                int Codeitem = DB.Probale.First(a => a.Prdno == prdno).CodeItem;
                var STATUS_LIST = (from s in DB.ItemMasterFile
                                   where s.CodeItem == Codeitem
                                   select new SelectListItem()
                                   {
                                       Text = s.Descriptionitem,
                                       Value = s.CodeItem.ToString(),
                                   }).ToList();
                return Json(STATUS_LIST, JsonRequestBehavior.AllowGet);
    
            }
    

    7

    Best regards,
    Lan Huang


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.