I have been attempting to fetch data for a dropdownlist using JSON. While the program is functioning correctly, the dropdownlist is displaying a blank list. Upon inspection, I can see that the data is present but not being displayed.
Controller:
public IActionResult GetClient()
{
var clientList = (from client in _context.Clients
select new SelectListItem()
{
Text = client.Nom,
Value = client.Id.ToString(),
}).ToList();
clientList.Insert(0, new SelectListItem()
{
Text = "----Select----",
Value = string.Empty
});
return Json(clientList);
}
Script:
<script type="text/javascript">
$(document).ready(function () {
$.ajax({
type: "GET",
url: "/Clients/GetClient",
success: function (data) {
$.each(data, function () {
$("#ClientId").append($("<option></option>").val(this['Value']).html(this['Text']));
});
}
});
});
</script>
HTML Code:
</div>
<div class="alert-danger" asp-validation-summary="ModelOnly"></div>
<label asp-for="ClientId"></label>
<select asp-for="ClientId"
class="form-control"
asp-items="@(new SelectList(Enumerable.Empty<SelectListItem>(),"Value", "Text"))">
</select>
</div>
<div>
Thank you for your assistance