JGDEV
JGDEV

Reputation: 35

MVC 4 Post not hitting controller/action

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!

Upvotes: 1

Views: 11456

Answers (1)

Shyju
Shyju

Reputation: 218842

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>

Upvotes: 8

Related Questions