美元.创建一个新表单



我想让用户能够创建一个新的游戏。这是我目前所知道的。我的$.post有问题。我该怎么做呢?

这是我的索引:

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <title></title>
    </head>
    <body>
        <form name="Name Of Game" action=$.post("data/games/create",  {title:""},function(data){});  method="get">
            New Game: <input type="text" name="game">
            <input type="submit" value="Create">
        </form>
    </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")]
        public Guid CreateGame(string title) {
            Game g = new Game() {
                ID = Guid.NewGuid(),
                Title = title
            };
            games.Add(g);
            return g.ID;
        }
    }
}

你应该为表单的提交添加一个eventListener。

$('form').on('submit', function (){
      //TODO: put params in variable here
      $.post("data/games/create",  params,function(data){})
});

最新更新