samedi 21 mai 2016

Populating HTML Table from JSON API in ASP.NET

So I am wanting to populate a table with items coming from a self made API. The API is working fine (showing / putting and deleting items,...), but I've failed to populate the existing table with the items.

What I'm using: - ASP.NET (MVC 5) - Entity Framework - JSON

What I have so far: url => /api/Film/ image I am asking this in a new thread since the solution should be working normally, I can't be bothered to learn this is in another method. I hope this won't be a problem.

My code:

Overview view:

<h3>Films</h3>
<table id="Films" border="1">
    <tr>
        <th>ID</th>
        <th>Titel</th>
        <th>Regisseur</th>
        <th>Ranking</th>
    </tr>
</table>

script:

 $(document).ready(function () {
            $.ajax({
                url: '/api/Film',
                type: 'GET',
                dataType: 'json',
                success: function (data) {
                    $.each(data, function (key, item) {
                        $('#Films').append("<tr><td>" + item.Id + "</td>" + "<td>" + item.Titel + "</td>" + "<td>" + item.Regisseur + "</td>" + "<td>" + item.Ranking + "</td><td><a href=\"#\" onclick=\"deleteFilm(" + item.Id + ")\">delete</a></tr>");
                    });
                }
            });

the (api)controller

// GET api/Film
public IEnumerable<Film> GetFilms()
{
    return db.Films.AsEnumerable();
}

What seems to be the problem in my code?

Aucun commentaire:

Enregistrer un commentaire