Dynamic Dropdowns NetCore and Json

 Html for placeholder

  <select id="AppList"  class="form-control input-sm"></select>


Controller returns  a Model - Add records to the Model and then return as JsonResult

 public async Task<JsonResult> GetApplications(int id)
{           var selectAppList = new List<ModelName>();
             var apps =  _applicationService.GetApplicationModels();
             var modelVariable = (await GetModelRecords);

 if (modelVariable is object )
{        
        selectAppList.Add(modelVariable);
}

     return Json(selectAppList);

 }

Jquery
Add something like this to document.ready

$("#brandId").change(function () {

var id = $("#brandId").val();

BuildAppList(id);

});

Then add the function

function BuildAppList(id) {

$.ajax({

type: 'GET',

url:'ConfigureKey/GetApplications/' + id,

data: { id: id },

success: function (result) {

$('#AppList').empty();

$.each(result, function (i, item) {

$('#AppList').append('<option value="' + item.modelid + '"> ' + item.name + ' </option>');

});

},

error: function (xhr, status, err) {

// error handling

}

});

}

Comments

Popular posts from this blog

Linq Exclude from separate list

Sorting Ascending and Descending

Linq Query Syntax vs Method Syntax