0

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?

1 Answer 1

2

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();
}
Sign up to request clarification or add additional context in comments.

1 Comment

@tatmanblue: Just for clarification. Html.BeginForm() will post to the currently rendered action, StartGame in this case. If you want to post somewhere else other than the current URL, then you need to specify the route as @mxmissile describes above.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.