0

Im making a website that eventually will be similar to Planning Poker. Currently what I am stuck on is being able to remove games once they are created. The problem i currently am having is calling gamesremoveALL from my controller.

Here is the code that Im having problem with ( its from my gamelist js)

 self.removeGames = function () {
    $.getJSON("/data/games/remove", function (d) {
        self.games.RemoveAll(d);
    })
}
};

Here is the rest of my code

Index( used to create games)

 <html>

<head>
<title>Planning Poker</title>
<style>
    .inlinetext {
        display: inline;
    }
</style>
<script src="Scripts/jquery-2.1.1.js"></script>
<script src="Scripts/knockout-3.1.0.js"></script>
<script type="text/javascript">
    $(function () {
        $('#button').on('click', function (data) {
            $.post('data/games/create/?title=5', function (d) { console.log(d) });
        })
    });
</script>
</head>



<body>
<h3 class='inlinetext'> Create Game: </h3>
    <input type="text" id="testtext" name="ime">
    <button id="button" >Create</button>


</body>

 </html>

Controller

using PlanningPoker.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;

namespace PlanningPoker.Controllers
{
public class GMController : ApiController
{
    private static List<Game> games = new List<Game>() {
            new Game() {
                ID = Guid.NewGuid(),
                Title = "D&D"
            }
        };

    [Route("data/games")]
    public IEnumerable<Game> GetAllGames() {
        return games;
    }

    [Route("data/games/create"), HttpPost]
    public Guid CreateGame(string title) {
        Game g = new Game() {
            ID = Guid.NewGuid(),
            Title = title
        };

        games.Add(g);

        return g.ID;
    }

    [Route("data/games/remove"), HttpPost]
    public void RemoveGame(Guid id) {
        games.RemoveAll(g => g.ID == id);
    }
}
}

GameList (js) This is where I am having the problem.

function AppViewModel() {
var self = this;

self.games = ko.observableArray([]);

$.getJSON("/data/games", function (d) {
    self.games(d);
});


self.removeGames = function () {
    $.getJSON("/data/games/remove", function (d) {
        self.games.RemoveAll(d);
    })
}
};
$(function () {
 ko.applyBindings(new AppViewModel());
 });

Gamelist (html)

 <html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Game List</title>
<script src="Scripts/jquery-2.1.1.js"></script>
<script src="Scripts/knockout-3.1.0.js"></script>
<script src="Gamelist.js"></script>
    </head>
    <body>

            <h4>Games</h4>

            <ul data-bind="foreach: $data.games">
                <li>
                    Game <span data-bind="text: $index"> </span>:
                    <span data-bind="text: Title"> </span>
                    <a href="#" data-bind="click: $parent.removeGames">Remove</a>
                </li>
            </ul>



    </body>
    </html>
  • You said you have a problem, what its the problem? (i.e. error response, details). – celerno Jun 18 '14 at 20:33
  • basicly my question is, what is the proper way to write this part self.removeGames = function () { $.getJSON("/data/games/remove", function (d) { self.games.RemoveAll(d); }) } }; So that it will call this part from my controller [Route("data/games/remove"), HttpPost] public void RemoveGame(Guid id) { games.RemoveAll(g => g.ID == id); } } } – user3662011 Jun 18 '14 at 20:43
  • Use Post for commands (Delete is a command) and Get for queries – Anders Jun 18 '14 at 21:28

2 Answers2

0
$.getJSON("/data/games/remove", function (d) {
    self.games.RemoveAll(d);
})

Your controller action has this route set as a POST route (HttpPost) to the CreateGame action. You cannot send a getJSON to that url. You need to do a post ajax call.

Also, you are not sending in the id value that the action is requesting. maybe something like:

$.ajax({
  url: '/data/games/remove',
  type: 'POST',
  data: id,
  dataType: 'json'
}).done(function(response) {
  //do something with response
});

This should allow it to successfully hit the action, pass in the id, and let the action handle the removal.

Tacoman667
  • 1,391
  • 9
  • 16
0

Assuming self.games is an observableArray, the method name is "removeAll", not "RemoveAll": http://knockoutjs.com/documentation/observableArrays.html

Joel Cochran
  • 7,139
  • 2
  • 30
  • 43