简体   繁体   中英

ASP.NET MVC 4 - Login on home page

I want a login form on my home page. In index.cshtml I have this:

@using (Html.BeginForm("Login", "AccountController", FormMethod.Post)) {
    @Html.AntiForgeryToken()
    @Html.ValidationSummary(true)
        <div>
            <div class="usernametb">
                @Html.LabelFor(m => m.LoginMod.UserName)
            </div>
            <div class="usernametb">
                @Html.TextBoxFor(m => m.LoginMod.UserName, new { style = "width:150px" })
                @Html.ValidationMessageFor(m => m.LoginMod.UserName)
            </div>
        </div>
        <div class="clear"></div>
        <div>
            <div class="pwtb">
                @Html.LabelFor(m => m.LoginMod.Password)
            </div>
            <div class="pwtb">
                @Html.PasswordFor(m => m.LoginMod.Password, new { style = "width:150px" })
                @Html.ValidationMessageFor(m => m.LoginMod.Password)
            </div>
        </div>
        <div class="clear"></div>
        <div class="rememberme">
            @Html.CheckBoxFor(m => m.LoginMod.RememberMe)
            @Html.LabelFor(m => m.LoginMod.RememberMe, new { @class = "checkbox" })
        </div>
        <div><input class="loginbutton" type="submit" value="Log In" /></div>
}

I reference this model:

namespace memsite.Models
{
    public class MemEvent
    {
        public LoginModel LoginMod { get; set; }
        public DateTime Start { get; set; }
        public DateTime End { get; set; }
        public string Name { get; set; }
        public string Description { get; set; }
    }
}

I am trying to hit this account controller login action, but it never hits the controller, it just re-directs to /AccountController/Login which doesn't exist. Also, the validation controls are not working...

namespace memsite.Controllers
{
    [Authorize]
    [InitializeSimpleMembership]
    public class AccountController : Controller
    {
        //
        // GET: /Account/Login

        [AllowAnonymous]
        public ActionResult Login(string returnUrl)
        {
            ViewBag.ReturnUrl = returnUrl;
            return View();
        }

        //
        // POST: /Account/Login

        [HttpPost]
        [AllowAnonymous]
        [ValidateAntiForgeryToken]
        public ActionResult Login(LoginModel model, string returnUrl)
        {
            if (ModelState.IsValid && WebSecurity.Login(model.UserName, model.Password, persistCookie: model.RememberMe))
            {
                return RedirectToLocal(returnUrl);
            }

            // If we got this far, something failed, redisplay form
            ModelState.AddModelError("", "The user name or password provided is incorrect.");
            return View(model);
        }

(I am brand new to MVC having left webforms land forever.)

Use Account instead of AccountController in your form. ASP MVC is based on naming conventions and the Controller is implied.

Change the name of the controller in below line

@using (Html.BeginForm("Login", "AccountController", FormMethod.Post)) {

To

@using (Html.BeginForm("Login", "Account", FormMethod.Post)) { 

because ASP.NET MVC will automatically detect anything ending with Controller via tthe routing configuration

The authentication section in Web.config specify you have a login type:

<authentication mode="Forms">
    <forms loginUrl="Account/Login" timeout="2880" />
</authentication>

Maybe your Web.config has a wrong configuration.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM