tatmanblue
tatmanblue

Reputation: 1227

ASP.NET MVC form input not working as expected

Trying to understand why my page doesn't work as expected. I was expecting the SignIn method on the controller to be called when clicking submit button, however, StartGame is still be called instead. The page originates through this URL: http://{domain}/Play/StartGame

Markup:

@{
    ViewBag.Title = "Start Game";
}

<h2>StartGame</h2>

@using (Html.BeginForm())
{
    @Html.TextBox("gamerId");
    <input type="submit" value="SignIn" class="btn btn-default" />
}

Controller:

public class PlayController : Controller
{
    public ActionResult Index()
    {
        return View();
    }

    public ActionResult StartGame()
    {
        return View();
    }

    public ActionResult SignIn(string gamerId)
    {
        return View();
    }
}

What am I missing here?

Upvotes: 0

Views: 214

Answers (1)

mxmissile
mxmissile

Reputation: 11681

You need to specify the action in your BeginForm().

@using (Html.BeginForm("SignIn","Play"))
{
    @Html.TextBox("gamerId");
    <input type="submit" value="SignIn" class="btn btn-default" />
}

Or another option is to create an overload action and use an attribute:

[HttpPost]
public ActionResult StartGame(string gamerId)
{
      return View();
}

Upvotes: 2

Related Questions