HOW TO FILL DROPDOWNLIST FROM SQL SERVER DATABASE IN ASP.NET

110 views
Skip to first unread message

Athirah Zain

unread,
Apr 25, 2021, 9:28:08 PM4/25/21
to KnockoutJS
Hi, 

I have issue with knockout used in MVC.net. My dropdown list do not have any values. 
And I am not sure what to put inside the <option> tag. 

Here is my html code: 

                <div class="col-xs-3 col-md-3 col-lg-3" style="padding-bottom: 15px">
                    <label>Select Package</label>  
                    <select class="form-control" data-bind="foreach: PackageList, value:SelectedPackage">
                        <option> ? </option>
                    </select>
                </div>

Here is my script: 

    self.PackageList = ko.observableArray();
    self.SelectedPackage = ko.observable();
    self.GetPackageList = function () {
        $.ajax({
            url: '/QC/GetPackageList',
            cache: false,
            type: 'GET',
            contentType: 'application/json; charset=utf-8',
            success: function (data) {
                self.PackageList(data.PackageList);
            }
        });
    }

And this is my controller: 

public ActionResult GetPackageList(string package)
        {
            var db = new SMLPORTALEntities();
            var packageList = db.SML_QC_SokuteiRejects.Select(a => a.Package == package).Distinct().ToList(); //Distinct: to avoid repetition 
            var result = new { Ok = "OK", PackageList = packageList};
            return Json(result, JsonRequestBehavior.AllowGet);
        }

Gunnar Liljas

unread,
Apr 26, 2021, 2:53:31 AM4/26/21
to knock...@googlegroups.com

--
You received this message because you are subscribed to the Google Groups "KnockoutJS" group.
To unsubscribe from this group and stop receiving emails from it, send an email to knockoutjs+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/knockoutjs/6b87b794-18b7-4da2-8e2d-ccbd8069c38bn%40googlegroups.com.

Jonathan Shay

unread,
Apr 26, 2021, 5:09:37 AM4/26/21
to knock...@googlegroups.com
You can either data-bind your individual options to properties of the items in your PackageList, or you can forego the options altogether and just do the binding on the select tag.

If you put a breakpoint in your ajax call, what does the PackageList variable contain?

Reply all
Reply to author
Forward
0 new messages