所以我做了一个用户可以登录的部分视图。
但是当我提交表格时,我会看到一个错误 “不允许子操作执行重定向操作。”
这是因为我的[HttpPost]控制器动作在控制器中处理部分视图重定向,显然是NoGo。
用户登录时我没有收到错误。要说明按下局部视图中的按钮的时间没有问题。
在部分视图中进行登录的原因是因为用户应该能够从应用程序中的任何页面登录
这是代码:
Controller for partialview Credentials:
public ActionResult Identify() {
if(Session["User"] != null) {
return PartialView("Identified", Session["User"]);
} else {
return PartialView();
}
}
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Identify(Login login) {
if(ModelState.IsValid) {
Authenticate(login.LoginName, login.LoginPasscode);
}
return Redirect(Request.UrlReferrer.AbsolutePath);
}
@model ViewModels.Login
@using(Html.BeginForm("Identify", "Credentials")) {
@Html.AntiForgeryToken()
@Html.ValidationSummary(true)
<div class="editor-label">
@Html.LabelFor(model => model.LoginName)
@Html.EditorFor(model => model.LoginName)
@Html.ValidationMessageFor(model => model.LoginName)
@Html.LabelFor(model => model.LoginPasscode)
@Html.EditorFor(model => model.LoginPasscode)
@Html.ValidationMessageFor(model => model.LoginPasscode)
<input class="button" type="submit" value="Log Ind" />
</div>
}
我已使用_Layout
中的以下行添加了局部视图 @{Html.RenderAction("Identify", "Credentials");}
HomeController中:
[HttpGet]
public ActionResult Index() {
db.Role.ToList();
return View();
}
[HttpPost]
public ActionResult Index(string Foo) {
db.Role.ToList();
ViewBag.InTheBox = Foo;
return View();
}
和上次主页索引视图:
@if (ViewBag.InTheBox == null){
ViewBag.InTheBox = "Nothing In Here";
}
<h1>@ViewBag.InTheBox</h1>
@using (Html.BeginForm("Index","Home")) {
@Html.AntiForgeryToken()
@Html.ValidationSummary(true)
<input type="text" name="Foo"/>
<input type="submit" value="Submit" />
}
修改
所以我想出了如何解决问题,这很简单。我的重载方法Identify应该只有一个httpget变体。在这种情况下,方法的httppost变体被重命名为Authenticate,表单发布到该方法。这解决了这个问题。正确的代码如下。
Controller for partialview Credentials:
public ActionResult Identify() {
if(Session["User"] != null) {
return PartialView("Identified", Session["User"]);
} else {
return PartialView();
}
}
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Authenticate(Login login) {
if(ModelState.IsValid) {
Authenticate(login.LoginName, login.LoginPasscode);
}
return Redirect(Request.UrlReferrer.AbsolutePath);
}
@model ViewModels.Login
@using(Html.BeginForm("Authenticate", "Credentials")) {
@Html.AntiForgeryToken()
@Html.ValidationSummary(true)
<div class="editor-label">
@Html.LabelFor(model => model.LoginName)
@Html.EditorFor(model => model.LoginName)
@Html.ValidationMessageFor(model => model.LoginName)
@Html.LabelFor(model => model.LoginPasscode)
@Html.EditorFor(model => model.LoginPasscode)
@Html.ValidationMessageFor(model => model.LoginPasscode)
<input class="button" type="submit" value="Log Ind" />
</div>
}
答案 0 :(得分:0)
尝试
RedirectToAcion(myActionName).