1

I'm getting a really annoying issue in MVC 4 where my form posts aren't hitting the action I've specified. I've done this loads of times before and it worked fine but in my new project it doesn't seem to work at all.

It's a simple login page, with nothing fancy, just a user name and password field. Here's my code

HTML/Razor:

@model XYZ.Models.PageModels.LoginPageModel

<div class="login">
    <form class="left form">
        @using (Html.BeginForm("Login", "Account", FormMethod.Post))
        {
            <div class="form-group form-group-sm">
                <label class="control-label" for="UserName">User Name</label>
                @Html.TextBoxFor(model => model.UserName, new {@class="form-control", @placeholder="User Name"})
            </div>


            <div class="form-group form-group-sm">
                <label class="control-label" for="Password">Password</label>
                @Html.PasswordFor(model => model.Password, new {@class="form-control", @placeholder="Password"})
            </div>


            <div class="buttons right">
                <input type="submit" class="btn btn-primary btn-sm" value="Login" />
            </div>
        }
    </form>
</div>

Controller:

public class AccountController : Controller
    {
        public ActionResult Login()
        {
            return View();
        }

        [HttpPost]
        public ActionResult Login(LoginPageModel model)
        {
            throw new Exception("I'm here!");
            return View();
        }
    }

Route config:

routes.MapRoute(
                name: "Default",
                url: "{controller}/{action}/{id}",
                defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
            );

Model:

public class LoginPageModel
    {
        [Required(ErrorMessage = "User Name required")]
        public string UserName { get; set; }

        [Required(ErrorMessage = "Password required")]
        public string Password { get; set; }
    }

Any Ideas? Thanks!

JGDEV
  • 35
  • 1
  • 1
  • 6

1 Answers1

8

You have nested forms in your markup. One you wrote yourself and the other one is generated from Html.BeginForm helper method. get rid of the outer one and you are good to go.

This should work fine.

<div class="login">
    @using (Html.BeginForm("Login", "Account"))
    {
        <div class="form-group form-group-sm">
            <label class="control-label" for="UserName">User Name</label>
            @Html.TextBoxFor(model => model.UserName, new { @class = "form-control", @placeholder = "User Name" })
        </div>


        <div class="form-group form-group-sm">
            <label class="control-label" for="Password">Password</label>
            @Html.PasswordFor(model => model.Password, new { @class = "form-control", @placeholder = "Password" })
        </div>


        <div class="buttons right">
            <input type="submit" class="btn btn-primary btn-sm" value="Login" />
        </div>
    }
</div>
Shyju
  • 214,206
  • 104
  • 411
  • 497
  • I can't believe that's all it was! I thought it was a div, not a form. Thank you, I'm now going to get an eye test. – JGDEV Apr 13 '15 at 21:46