1

I am having some trouble in ASP.NET MVC4 - When I click the Login button it's not hitting my controller and not logging in

This is the code on my .cshtml

@using System.Linq

<body>
    <div class="container">
        @using (Html.BeginForm("Login", "Login", FormMethod.Post, new { @Class = "form-signin", enctype = "multipart/form-data" }))
        {
            @Html.AntiForgeryToken()
            @Html.ValidationSummary(true, "Login failed. Check your login details.")
            <img class="img-responsive" src="~/Images/PI%20Logo.jpg" />
            @Html.TextBoxFor(m => m.userName, new {@Class = "form-control", @Id = "user", @placeholder = "Username"})
            @Html.ValidationMessageFor(m => m.userName)
            @Html.PasswordFor(p => p.passwd, new {@Class = "form-control", @Id = "pass", @placeholder = "Password"})
            @Html.ValidationMessageFor(m => m.passwd)
            <!--<input class="form-control" id="username" placeholder="Username" type="text" />
            <input class="form-control" id="Password1" placeholder="Password" type="password" /> -->
            <input id="submit" class="btn btn-lg btn-primary btn-block" type="button" value="LOGIN" />
        }
    </div>

    <script src="~/Scripts/jquery-1.11.2.min.js"></script>
    <script src="~/Scripts/bootstrap.min.js"></script>
</body>

and this is my controller

public class LoginController : Controller
{
    //
    // GET: /Login/

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

    [ValidateAntiForgeryToken]
    [HttpPost]
    public ActionResult Login(Login login)
    {
        AccountManagement am = new AccountManagement();
        var xrm = new XrmServiceContext("Xrm");

        SystemUser sysUser = xrm.SystemUserSet.Where(x => x.DomainName == "hc\\" + login.userName && x.IsDisabled == false).FirstOrDefault();

        if (am.ValidateCredentials(login.userName, login.passwd) == "True" && sysUser != null)
        {
            Session["username"] = login.userName;
            return RedirectToAction("MainHome", "MainMenu");//Request.CreateResponse(HttpStatusCode.OK, new { Message = "Success", User = sysUser });
        }
        else
        {
            ModelState.AddModelError("", "Login data is incorrect!");//Request.CreateErrorResponse(HttpStatusCode.Unauthorized, "Username or Password Invalid");
        }
        return View(login);
    }

}

What's wrong with my code - i'm so confused, because many tutorial made simple login like this but it's work

1
  • If you want to submit the form it should be <input type="submit" ..> Commented Apr 14, 2015 at 8:48

1 Answer 1

2

Change button type to submit

<input id="submit" class="btn btn-lg btn-primary btn-block" type="submit" value="LOGIN" />

Difference between input type Button & submit

<input type="button" /> 

buttons will not submit a form - they don't do anything by default. They're generally used in conjunction with JavaScript as part of an AJAX application.

<input type="submit">

buttons will submit the form they are in when the user clicks on them, unless you specify otherwise with JavaScript.

Sign up to request clarification or add additional context in comments.

1 Comment

Nice - fast answer ;) - want to explain why it wasn't working for them ?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.