As the title says I am having a bit of trouble with this. I have succeeded hardcoding the JSON but once I begin to use a model it goes wrong. Please see what I have to-date below.
Ajax
$(document).ready(function () {
$.getJSON('/Programs/GetAll', function (data) {
//clear the current content of the select
$('#ProgramId').empty();
//iterate over the data and append a select option
$.each(data, function (key, val) {
$('#ProgramId').append('<option id="' + key + '">' + val + '></option>');})
});
});
Controller
[HttpGet]
public JsonResult GetAll()
{
var progams = _context.Program
.Select(a => new
{
id = a.Id.ToString(),
name = a.Name
}).ToList();
return Json(progams);
}
This hardcoding works with the ajax, js and html.
[HttpGet]
public JsonResult Get()
{
Dictionary<string, string> programSelectList = new Dictionary<string, string>();
programSelectList.Add("1", "Bob");
programSelectList.Add("2", "Cratchett");
return Json(programSelectList);
//return Json(_context.Program.ToJson());
}
I am missing something basic, no doubt, and any help would be greatly appreciated.
Thanks.

