Populate dropdown list based on data retrieved from database in MVC Application (VIEW)

View

jquery

    function getRelation() {
        $.ajax
            ({
                url: "@(Url.Action("FillRoles", "Roles"))",
                data: {},
                success: function (result) {
                    var html = "";
                    html += "<option>Select</option>";
                    for (var i = 0; i < result.length; i++) {
                        html += "<option value=" + result[i].ID + ">" + result[i].Description + "</option>";
                    }
                    $("#ddlAddRoles").html(html);
                }
            });
    }

html


<div class="col-sm-4 tb-font">
<form>
   <label class="col-form-label">Relation</label>
   <select id="ddlAddRoles" name="ddlAddRoles" class="form-control dropdown" style="margin-bottom:2px"></select>
 </form>
 </div>

Explanation

This function will receive data returned from FillRoles() Method of Roles Controller and populate the append the ddlAddRoles dropdown list in HTML

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Create a free website or blog at WordPress.com.

Up ↑

Design a site like this with WordPress.com
Get started