我使用登录表单创建了一个网站。首先,我创建登录表单只是为了在帐户/登录下进行测试。然后一些需要更改所有页面应该显示登录。所以我创建了一个部分并在 _Layout.cshtml 下面调用,
<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>
}
但是我无法登录。如果我使用单独的页面(LogOn.cshtml)登录,则意味着它已成功记录。我不知道为什么会发生......任何人都可以澄清我。
最佳答案
您需要在表单中显式指定 Controller 操作:
@using (Html.BeginForm("LogOn", "Account")) {
...
}
这样做的原因是,当您使用不带任何参数的 Html.BeginForm()
帮助程序时,它会使用当前 url 作为表单的操作。但当前的 url 可能完全不同,因为该 View 可以从任何 Controller 提供。通过显式指定您希望将表单提交到的 Controller 操作,帮助器将生成正确的操作属性:
<form action="/Account/LogOn" method="post">
...
</form>
关于asp.net-mvc - 从部分 View mvc razor 获取值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15877311/