首先,我是 MVC 的新手,我是一个 webforms 人......
我想在我的home/index
页面上显示我的登录控件,但无论我做什么,我都会遇到错误。我尝试了各种技术,每种技术都有很多错误,所以我不会在这里一一列举。
现在我已经设置了,如果Request.IsAuthenticated
然后显示用户名,则显示登录表单。但是表单显然正在查看家庭控制器,当我提交登录时没有任何反应。
任何建议将不胜感激。这几天我一直在涉猎。
谢谢 :)
这是代码:
'@{
ViewBag.Title = "MyApp";
}
<p>Code for main Index Page here</p>
@model Application.Models.LoginModel
@{
ViewBag.Title = "Log in";
}
@if (Request.IsAuthenticated) {
<text>
Hello, @Html.ActionLink(User.Identity.Name, "Manage", "Account", routeValues: null, htmlAttributes: new { @class = "username", title = "Manage" })!
@using (Html.BeginForm("LogOff", "Account", FormMethod.Post, new { id = "logoutForm" })) {
@Html.AntiForgeryToken()
<a href="javascript:document.getElementById('logoutForm').submit()">Log off</a>
}
</text>
} else {
<section id="loginForm">
<h2>Use a local account to log in.</h2>
@using (Html.BeginForm(new { ReturnUrl = "RedirectToAction" }))
{
@Html.AntiForgeryToken()
@Html.ValidationSummary(true)
<fieldset>
<legend>Log in Form</legend>
<ol>
<li>
@Html.LabelFor(m => m.UserName)
@Html.TextBoxFor(m => m.UserName)
@Html.ValidationMessageFor(m => m.UserName)
</li>
<li>
@Html.LabelFor(m => m.Password)
@Html.PasswordFor(m => m.Password)
@Html.ValidationMessageFor(m => m.Password)
</li>
<li>
@Html.CheckBoxFor(m => m.RememberMe)
@Html.LabelFor(m => m.RememberMe, new { @class = "checkbox" })
</li>
</ol>
<input type="submit" value="Log in" />
</fieldset>
<p>
@Html.ActionLink("Register", "Register", "Account") if you don't have an account.
</p>
}
@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
}
</section>
}
'