Home > other >  How to show Selected value from dropdown inside table in dot net core mvc with jquery
How to show Selected value from dropdown inside table in dot net core mvc with jquery

Time:12-24

'''
$.ajax({
            type: "POST",
            url: '@Url.Action("GridData", "PeriodOpenclose")/',
            dataType: "json",
              success: function (response) {
                  debugger
                if (response.data.length > 0) {
                    $.each(response.data, function (i, List) {
                        var newRow = $("<tr>");
                        var cols = "";
cols  = '<td> <select id="dropdowntype"  ><option value="1">Open </option> <option value="2">Close </option></select ></td > ';
                        $("#dropdowntype").val($("#dropdowntype option:selected").val(List.periodstatus));
'''

I added this inside table i have added select tag and Manually option is added. i am selecting value 2 in List.periodstatus,i am unable to display selected value in dropdown.

CodePudding user response:

If you want to set selected option of <select></select> which id is dropdowntype,you only need to change cols,and then add cols to html.

var cols = "";

            cols  = '<select id="dropdowntype"  >';
            for (var j = 1; j <= 2; j  ) {
                if (j == List.periodstatus) {
                    cols  = '<option value="'   j   '" selected>Open </option>';
                } else {
                    cols  = '<option value="'   j   '">Open </option>';
                }
            }
            cols  = '</select >';

After using the code,selected will added to the option which value is List.periodstatus in cols.

  • Related