Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何从控制器调用方法_Javascript_Asp.net_Knockout.js - Fatal编程技术网

Javascript 如何从控制器调用方法

Javascript 如何从控制器调用方法,javascript,asp.net,knockout.js,Javascript,Asp.net,Knockout.js,我正在制作一个网站,最终将类似于规划扑克。目前我一直坚持的是,一旦游戏被创建,就可以将其删除。我目前遇到的问题是从我的控制器调用gamesremoveALL 下面是我遇到问题的代码(来自我的游戏列表js) 这是我剩下的代码 索引(用于创建游戏) 游戏列表(html) 游戏列表 游戏 游戏: 控制器操作将此路由设置为CreateGame操作的POST路由(HttpPost)。无法向该url发送getJSON。您需要执行一个post-ajax调用 此外,您没有发送操作请求的id值。可能是

我正在制作一个网站,最终将类似于规划扑克。目前我一直坚持的是,一旦游戏被创建,就可以将其删除。我目前遇到的问题是从我的控制器调用gamesremoveALL

下面是我遇到问题的代码(来自我的游戏列表js)

这是我剩下的代码

索引(用于创建游戏)

游戏列表(html)


游戏列表
游戏
  • 游戏:
控制器操作将此路由设置为CreateGame操作的POST路由(HttpPost)。无法向该url发送getJSON。您需要执行一个post-ajax调用

此外,您没有发送操作请求的id值。可能是这样的:

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

这将允许它成功命中操作,传递id,并让操作处理删除操作。

假设self.games是一个可观察的array,方法名称为“removeAll”,而不是“removeAll”:

你说你有问题,有什么问题?基本上我的问题是,编写这部分self.removeGames=function(){$.getJSON(“/data/games/remove”,function(d){self.games.RemoveAll(d);}}};因此,它将从我的控制器[Route(“data/games/remove”),HttpPost]public void RemoveGame(Guid id){games.RemoveAll(g=>g.id==id);}}}}}}对命令使用Post(Delete是一个命令),对querytype使用Get应该设置为
Delete
Yes。我只是想让OP了解原因。HttpDelete并键入:“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>
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);
    }
}
}
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());
 });
 <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>
$.getJSON("/data/games/remove", function (d) {
    self.games.RemoveAll(d);
})
$.ajax({
  url: '/data/games/remove',
  type: 'POST',
  data: id,
  dataType: 'json'
}).done(function(response) {
  //do something with response
});