I created one website using login form. First I create the login form just for test under the Account/Logon.. Then some needs to change all pages should show login. So i create one partial and call at _Layout.cshtml following,
<div id="logindisplay">
@{ Html.RenderPartial("_LogOnPartial"); }
</div>
_LogOnPartial.cshtml:
@model HOP2013.Models.LogOnModel
@{
ViewBag.Title = "Log On";
}
@if(Request.IsAuthenticated) {
<text>Welcome <b>@Context.User.Identity.Name</b>!
[ @Html.ActionLink("Log Off", "LogOff", "Account") ]</text>
}
else {
@: @Html.ActionLink("Log On", "LogOn", "Account")
}
@Html.ValidationSummary(true, "Login was unsuccessful. Please correct the errors and try again.")
@using (Html.BeginForm()) {
<fieldset>
<legend>Account Information</legend>
<div id="user" class="user">
<div class="editor-label">
@Html.LabelFor(m => m.UserName)
</div>
<div class="editor-field">
@Html.TextBoxFor(m => m.UserName, new { @title = "UserName" })
@Html.ValidationMessageFor(m => m.UserName)
</div>
</div>
<div id="password" class="password">
<div class="editor-label">
@Html.LabelFor(m => m.Password)
</div>
<div class="editor-field">
@Html.PasswordFor(m => m.Password, new { @title = "UserName" })
@Html.ValidationMessageFor(m => m.Password)
</div>
<div class="editor-label">
@Html.CheckBoxFor(m => m.RememberMe)
@Html.LabelFor(m => m.RememberMe)
</div>
</div>
<p>
<input type="submit" class="login" value="Log On" />
</p>
<p>New @Html.ActionLink("Register", "SignUp", "Account")Here</p>
</fieldset>
}
But I can't login. If i login with the separate page (LogOn.cshtml) means it is logging successfully. I don't know why it is happening..Anyone clarify me.
You need to explicitly specify the controller action in your form:
@using (Html.BeginForm("LogOn", "Account")) {
...
}
The reason for that is because when you use the Html.BeginForm()
helper without any parameters it uses the current url as action of the form. But the current url could be something entirely different because this view could be served from any controller. By explicitly specifying the controller action you want the form to be submitted to, the helper will generate the proper action attribute:
<form action="/Account/LogOn" method="post">
...
</form>