—>To bind table values to dropdownlist first create table in database with some records.
—>In view write logic to create a label with dropdownlist as below.
<td>
<label for=”State”><b>State</b></label>
<select class=”container” id=”state” name=”stateDropdown”></select>
</td>
—>In controller write logic as below.
[HttpGet]
public JsonResult States()
{
return Json(itContext.States(), JsonRequestBehavior.AllowGet);
}
—>In view under Script write logic as below.
- $(document).ready(function () {
- $.ajax({
- type: “GET”,
- url: “/State/States”,
- data: “{}”,
- success: function (data) {
- var s = ‘<option value=”-1″>Please Select a State</option>’;
- for (var i = 0; i < data.length; i++) {
- s += ‘<option value=”‘ + data[i].StateID + ‘”>’ + data[i].Name + ‘</option>’;
- }
- $(“#state”).html(s);
- }
- });
- });
—>Finally databasevalues bind to dropdownlist as below.
—>Link