Remove Function Javascript, knockout, c#, asp.net

Remove Function Javascript, knockout, c#, asp.net

本文关键字:asp net knockout Javascript Remove Function      更新时间:2023-09-26

嗨,我正在尝试创造一些类似于计划扑克的东西。目前我有它,所以你可以创建一个游戏,我正试图使它,所以你可以删除一个游戏。目前我的删除删除游戏,但只是暂时的,如果你点击刷新,他们仍然在那里。我知道我要做什么,只是不知道怎么做。我需要delete按钮从控制器的可观察数组中删除游戏。

我的代码

索引(创建游戏)

<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>

游戏列表(一个列出游戏的页面,我希望能够从这个列表中删除游戏)

<!DOCTYPE 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>

Gamelist.js

function AppViewModel() {
var self = this;
self.games = ko.observableArray([]);
$.getJSON("/data/games", function (d) {
    self.games(d);
 });

 self.removeGames = function () {
    self.games.remove(this);
    }
}
$(function () {
ko.applyBindings(new AppViewModel());
});
控制器

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;
    }
}

Knockout教程的第4步演示了您要实现的目标。

self.removeGames = function(game) { self.games.remove(game) }