试图理解为什么我的网页无法正常工作。我期待在单击提交按钮时调用控制器上的SignIn方法,但是,仍然会调用StartGame。该网页来自以下网址:http:// {domain} / Play / StartGame
标记:
@{
ViewBag.Title = "Start Game";
}
<h2>StartGame</h2>
@using (Html.BeginForm())
{
@Html.TextBox("gamerId");
<input type="submit" value="SignIn" class="btn btn-default" />
}
控制器:
public class PlayController : Controller
{
public ActionResult Index()
{
return View();
}
public ActionResult StartGame()
{
return View();
}
public ActionResult SignIn(string gamerId)
{
return View();
}
}
我在这里缺少什么?
答案 0 :(得分:2)
您需要在BeginForm()
中指定操作。
@using (Html.BeginForm("SignIn","Play"))
{
@Html.TextBox("gamerId");
<input type="submit" value="SignIn" class="btn btn-default" />
}
或另一种选择是创建一个重载动作并使用一个属性:
[HttpPost]
public ActionResult StartGame(string gamerId)
{
return View();
}