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!

2
  • looks like a missing directive is your controller Apr 13, 2015 at 21:34
  • Not sure if this helps or makes a difference, but the project also includes Web API.
    – JGDEV
    Apr 13, 2015 at 21:38

1 Answer 1

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>
1
  • 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, 2015 at 21:46

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.