我是MVC的新手,我想实现部分LogIn,我可以在几个视图中使用它。但我有一个问题我不明白。
因此,在/views/shared/_LogOnPartial.cshtml中,我有以下代码:
@using netlek.ViewModels.Account
@model LogOnModel
<div class="widget-main padding-6">
@using (Html.BeginForm("LogOn", "Account", new { ReturnUrl = @Request.QueryString["ReturnUrl"] }))
{
@Html.ValidationSummary(true, "Oops, that didn't work.", new Dictionary<string, object>() { })
<div class="loginctr">
<fieldset class="account">
<label class="block clearfix">
<span class="input-icon input-icon-right">
@Html.TextBoxFor(m => m.UserName, new { placeholder = "Username / Email" })
</span>
<span class="block">@Html.ValidationMessageFor(m => m.UserName)</span>
</label>
<label class="block clearfix">
<span class="input-icon input-icon-right">
@Html.PasswordFor(m => m.Password, new { placeholder = "Password" })
</span>
<span class="block">@Html.ValidationMessageFor(m => m.Password)</span>
</label>
<div class="fl">
<input type="submit" value="Log On" class="btn btn-primary btn-large" style="margin: 20px 0;" />
</div>
</fieldset>
</div>
}
</div>这是发布到/Account/LogOn (AccountController)的简单Post表单:
public class AccountController : ControllerBase
{
[HttpPost]
public ActionResult LogOn(LogOnModel model, string returnUrl)
{
model.NoCcRequired = !ApplicationSettings.RequireCcUpfront;
if (ModelState.IsValid)
{
if (Membership.ValidateUser(model.UserName, model.Password))
{
TrackingTasks.SetUserAction(MvcApplication.DbSession, model.UserName, TrackingActionType.LoggedIn, TrackingPropertyType.LastLoginDate, DateTime.UtcNow);
FormsAuthentication.SetAuthCookie(model.UserName, model.RememberMe);
if (Url.IsLocalUrl(returnUrl) && returnUrl.Length > 1 && returnUrl.StartsWith("/")
&& !returnUrl.StartsWith("//") && !returnUrl.StartsWith("/\\"))
{
return this.RedirectToFirstPage(model.UserName, returnUrl);
}
return this.RedirectToFirstPage(model.UserName, string.Empty);
}
var user = Membership.GetUser(model.UserName);
if (user != null && user.IsLockedOut)
{
ModelState.AddModelError(string.Empty, "To protect your account, you have been locked out due to too many failed login attempts. Please contact support.");
return this.View(model);
}
ModelState.AddModelError(string.Empty, "The user name or password provided is incorrect.");
}
// If we got this far, something failed, redisplay form
return this.View(model);
}
}现在我想在两个不同的页面(两个视图模型)中使用这个部分登录:
/views/Account/LogOn.cshtml (标准LogOn页面):
@using netlek.ViewModels.Account
@model LogOnModel
@{
Layout = "~/Views/Shared/_NoHeaderLayout.cshtml";
ViewBag.Title = "Log On";
}
//SOME HTML BEFORE
<div class="span4 align-left">
@Html.Partial("_LogOnPartial")
</div>
//SOME HTML AFTER我想在另一个页面(另一个视图)中使用它
/views/Extension/LogOn.cshtml:
@using netlek.ViewModels.Account
@model LogOnModel
@{
Layout = "~/Views/Shared/_NoHeaderLayout.cshtml";
ViewBag.Title = "Log On";
}
<div id="logon-wrap" class="span4">
@Html.Partial("_LogOnPartial")
</div>我有的问题是,如果我访问第二个LogOn页面(扩展/登录),并提交没有用户名和密码的登录表单,因此它失败。它命中了AccountController中的方法LogOn(),由于失败,我返回this.View( LogOn );它会将我重定向到标准的登录页面(/Account/LogOn)。
因此,如果我访问第二个页面,其中包含此部分LogOn:
/External/LogOn和我点击LogIn按钮,但没有任何详细信息,它失败并将我重定向到/Account/LogOn页面,但我想留在同一页面/External/LogOn。
我理解为什么会发生这种情况,因为它正在访问AccountControler.LogOn(),它返回View(模型),它返回"View/Account/LogOn.cshtml“。但是我不知道如何正确地修复/实现这个问题。
因此,我希望这个部分登录在几个视图中使用,以防它无法返回我提交的表单not always /Account/LogOn view。
发布于 2016-05-09 17:07:57
您可以做的是从AccountController继承ExternalController类,并修改局部视图以post到LogOn操作。现在,由于LogOn方法也存在于ExternalController类中,因此返回View(model)将返回External的登录视图。(您将扩展和外部混合在一起,但我假设它们是相同的)。
代码的其他一些备注使它们更安全:检查returnUrl是否在您的域中,并包含一个防伪令牌,以防止您的站点受到CSRF攻击。
发布于 2016-05-09 17:23:08
尝试像这样更改控制器参数,
public ActionResult LogOn(LogOnModel model, string returnUrl, string view = string.Empty)
{
// Your code here and then write below condition
if(view == "External")
return RedirectToAction("Extension/LogOn");
else
return View(model);
}现在从父视图调用分部视图,如下所示;
/views/Account/LogOn.cshtml (标准LogOn页面):
@using netlek.ViewModels.Account
@model LogOnModel
@{
Layout = "~/Views/Shared/_NoHeaderLayout.cshtml";
ViewBag.Title = "Log On";
}
//SOME HTML BEFORE
<div id="LogOn" class="span4 align-left">
// @Html.Partial("_LogOnPartial")
</div>
<script>
$(document).ready(function () {
$("#LogOn").load("/LogOnPartial/", { view: "Account" });
});
</script>你的另一个页面将会是;
/views/Extension/LogOn.cshtml:
@using netlek.ViewModels.Account
@model LogOnModel
@{
Layout = "~/Views/Shared/_NoHeaderLayout.cshtml";
ViewBag.Title = "Log On";
}
<div id="logon-wrap" class="span4">
// @Html.Partial("_LogOnPartial")
</div>
<script>
$(document).ready(function () {
$("#logon-wrap").load("/LogOnPartial/", { view: "External" });
});
</script>https://stackoverflow.com/questions/37105841
复制相似问题