Minifycode 2021-10-03 Viewed 1.3K times ASP.Net Core MVC

In this article, you will learn how to bind data in drop down list in jQuery .net core

 

<div class="col-md-4">
                    <div class="form-group">
                        <label>Nationality</label>
                        <select class="form-control ddl" id="ddl_nationality" name="ddl_nationality" style="width:100%" data-toggle="select2" data-placeholder="Choose ...">
                           
                        </select>
                    </div>
                </div>

 

 public IActionResult CountryMaster()
        {
           
                var dt = dal.GetList("table Name", "*", "Status='0' ORDER BY CountryName ASC");
                return Json(Lookup.DataTableToJSONWithJSONNet(dt));
          
        }

 

.cs class

 

 public static string DataTableToJSONWithJSONNet(DataTable table)
        {
            string JSONString = string.Empty;
            JSONString = JsonConvert.SerializeObject(table);
            return JSONString;
        }

 

<script>
    $(document).ready(function () {
        /**************************************bind Dropdown Country Master***********************************/
        $.get("/Home/CountryMaster", {}, function (data) {
            $.each(JSON.parse(data), function (i, v) {
                if (v.CountryName == 'India') {
                    $('#ddl_nationality').append('<option selected="selected" value="' + v.ID + '">' + v.CountryName + '</option>');                   
                } else {
                    $('#ddl_nationality').append('<option value="' + v.ID + '">' + v.CountryName + '</option>');
                }
            })
        });
    });
</script>

How to bind data in drop down list in jQuery .net core
minify code