0

I have a Partial view (Login, with username, password and Submit button), and the partial view is being used on my _layout (materpage).

So, on my _layout page, I have:

<div style="text-align: right">
    @Html.Partial("_LoginPartial")
</div>

My _LoginPartial has the following code:

    @if (Request.IsAuthenticated)
{
    <textarea>Welcome!
        [ @Html.ActionLink("Log Off", "Logout", "Account")]</textarea>

}
else
{
    @Html.Partial("~/Views/Account/Index.cshtml")
}

The Index file to display the login box looks like this:

  @using GalleryPresentation.Models
@model LoginModel

<script src="../../Scripts/jquery.validate.min.js" type="text/javascript"></script>
@using (Html.BeginForm("index", "Account"))
{
    <table>
        <tr>
            <td>@Html.LabelFor(m => m.Username)</td>
            <td>@Html.TextBoxFor(m => m.Username)</td>
        </tr>
        <tr>
            <td>@Html.LabelFor(m => m.Password)</td>
            <td>@Html.PasswordFor(m => m.Password) kjkj</td>
        </tr>
        <tr>
            <td colspan="2"><input type="submit" value="Login"/></td>
        </tr>
        <tr>
            <td colspan="2">@Html.ValidationSummary()</td>
        </tr>
    </table>

}

In my AccountCOntroller, I have the following code:

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

    [HttpPost]
    public ActionResult Index(LoginModel loginModel)
    {
        if(ModelState.IsValid)
        {
            var g = new GallaryImage();
            var user = g.LoginUser(loginModel.Username, loginModel.Password);
            if(user != null) 
            {
                FormsAuthentication.SetAuthCookie(user.username, false);
                return RedirectToAction("Index", "Home");
            }
            ModelState.AddModelError("", "Invalid Username/Password");
        }
        return View(loginModel);
    }

    public ActionResult Logout()
    {
        FormsAuthentication.SignOut();
        return RedirectToAction("Index", "Home");
    }

I have breakpoints on all methods - but they never get hit. Pressing the submit button simply changes my URL to:

http://localhost:8741/?Username=myusername&Password=mypassword

Can anyone spot the error I am making?

Craig
  • 18,074
  • 38
  • 147
  • 248

1 Answers1

1

Since Html.BeginForm defaults to making GET requests, you are making a GET request with from your view. However, your action only accepts POST requests.

You can change @using (Html.BeginForm("index", "Account")) to @using (Html.BeginForm("index", "Account", FormMethod.Post)).

Stephen Booher
  • 6,522
  • 4
  • 34
  • 50
  • Tried that, but still failing to hit a breakpoint. I thought form posts defaulted to post though? – Craig Jan 14 '12 at 05:46
  • @Craig If the form is redirecting you to `http://localhost:8741/?Username=myusername&Password=mypassword`, then that is indicative of a HTTP GET request, since browsers convert GET form submissions to query parameters. To assist with debugging, you could try inspecting the network request that the browser makes. You can do this with the developer tools that your browser comes with, or a third-party tool like Fiddler. – Stephen Booher Jan 14 '12 at 07:31
  • Thanks Stephen - fiddler helped. I found that it came down to my _layout having for
    tag, and that was killing my form tag on my login box. Fixed. Thanks.
    – Craig Jan 14 '12 at 19:54