C# ASP.NET MVC 3表单提交影响渲染处理程序
我可以控制用户授权,包括表单、两个文本框和提交按钮。 此控件通过RenderAction方法包含在母版页中。 我的注册页面(它的视图通过RenderBody方法包含)也带有表单。当我从注册表提交数据时,登录控件也会被触发,并调用其处理程序(用于处理POST数据的控制器方法)。下面您可以看到用于授权的控制器方法。 在从其他表单提交数据后,如何防止向登录控件发送POST数据C# ASP.NET MVC 3表单提交影响渲染处理程序,c#,asp.net-mvc-3,C#,Asp.net Mvc 3,我可以控制用户授权,包括表单、两个文本框和提交按钮。 此控件通过RenderAction方法包含在母版页中。 我的注册页面(它的视图通过RenderBody方法包含)也带有表单。当我从注册表提交数据时,登录控件也会被触发,并调用其处理程序(用于处理POST数据的控制器方法)。下面您可以看到用于授权的控制器方法。 在从其他表单提交数据后,如何防止向登录控件发送POST数据 [HttpPost] public RedirectResult LogIn(AuthView
[HttpPost]
public RedirectResult LogIn(AuthViewModel authResult)
{
if (ModelState.IsValid)
{
userService.LogInUser(authResult.Login, authResult.Password, Request.UserHostAddress);
}
else
{
TempData["AuthMessage"] = GetValidationMessage();
}
string redirectUrl = "/";
if (Request.UrlReferrer != null)
{
redirectUrl = Request.UrlReferrer.AbsoluteUri.ToString();
}
return Redirect(redirectUrl);
}
[HttpGet]
[ChildActionOnly]
public PartialViewResult LogIn()
{
if (userService.IsUserLoggedIn())
{
User currentUser = userService.GetLoggedInUser();
ViewBag.LoggedInMessage = currentUser.FullName + "(" + currentUser.Login + ")";
}
return PartialView("AuthControl");
}
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<title>@ViewBag.Title</title>
</head>
<body>
<div>
<div id="header">
<div>
<div>
@{Html.RenderPartial("SearchControl");}
</div>
</div>
</div>
<div id="right_menu">
<div>
@{Html.RenderAction("LogIn", "Navigation");}
</div>
@{Html.RenderAction("Menu", "Navigation");}
<div>
@{Html.RenderAction("Index", "Messages");}
</div>
<div>
@{Html.RenderAction("TagCloud", "Navigation");}
</div>
</div>
<div id="main_content">
@RenderBody()
</div>
<div id="footer">
</div>
</div>
</body>
</html>
[HttpPost]
公共重定向结果登录(AuthViewModel authResult)
{
if(ModelState.IsValid)
{
userService.LogInUser(authResult.Login、authResult.Password、Request.UserHostAddress);
}
其他的
{
TempData[“AuthMessage”]=GetValidationMessage();
}
字符串重定向URL=“/”;
if(Request.urlReferer!=null)
{
redirectUrl=Request.urlReferer.AbsoluteUri.ToString();
}
返回重定向(重定向URL);
}
[HttpGet]
[仅限儿童]
public PartialViewResult登录名()
{
if(userService.IsUserLoggedIn())
{
User currentUser=userService.GetLoggedInUser();
ViewBag.LoggedInMessage=currentUser.FullName+”(“+currentUser.Login+”);
}
返回PartialView(“AuthControl”);
}
@视图包。标题
@{Html.RenderPartial(“SearchControl”);}
@{Html.RenderAction(“登录”、“导航”);}
@{Html.RenderAction(“菜单”、“导航”);}
@{Html.RenderAction(“索引”、“消息”);}
@{Html.RenderAction(“标记云”、“导航”);}
@RenderBody()
授权控制:
@model AuthViewModel
<div class="rounded-corners auth-panel">
@if (ViewBag.LoggedInMessage == null)
{
<div class="auth-container">
@using (Html.BeginForm("LogIn", "Navigation"))
{
<div>
<label for="Login">
Login:
</label>
@Html.TextBoxFor(m => m.Login, new { @class="middle-field"})
</div>
<div>
<label for="Password">
Password:
</label>
@Html.PasswordFor(m => m.Password, new { @class="middle-field"})
</div>
<div class="in-center">
<input type="image" src="@Url.Content("~/Content/Images/submit.png")"/>
</div>
}
</div>
<div class="error-msg">
@if (TempData["AuthMessage"] != null)
{
@Html.Raw(TempData["AuthMessage"].ToString())
}
@Html.ValidationSummary()
</div>
<div class="small-nav-message">
<a href="#" class="registration-link">Registration</a>
</div>
}
</div>
@model AuthViewModel
@if(ViewBag.LoggedInMessage==null)
{
@使用(Html.BeginForm(“登录”、“导航”))
{
登录:
@TextBoxFor(m=>m.Login,新的{@class=“middle field”})
密码:
@Html.PasswordFor(m=>m.Password,新的{@class=“middle field”})
}
@if(TempData[“AuthMessage”]!=null)
{
@Html.Raw(TempData[“AuthMessage”].ToString())
}
@Html.ValidationSummary()
}
注册页面:
RegistrationViewModel
@{
ViewBag.Title = "Registration";
}
@if (TempData["RegistrationFinished"] == null || !(bool)TempData["RegistrationFinished"])
{
<div class="post content-holder">
<div class="fields-holder">
<div >
<div class="error-msg">
@if (TempData["ValidationMessage"] != null)
{
@Html.Raw(TempData["ValidationMessage"].ToString())
}
</div>
@using (Html.BeginForm())
{
<span>
Email:
</span>
<span>
@Html.TextBoxFor(v => v.Email)
</span>
<span>
Password:
</span>
<span>
@Html.PasswordFor(v => v.Password)
</span>
<input type="submit" value="Register"/>
}
</div>
</div>
</div>
}
else
{
<div>
Activation link was sent to your email.
</div>
}
RegistrationViewModel
@{
ViewBag.Title=“注册”;
}
@如果(TempData[“RegistrationFinished”]==null | |!(bool)TempData[“RegistrationFinished”])
{
@if(TempData[“ValidationMessage”]!=null)
{
@Html.Raw(TempData[“ValidationMessage”].ToString())
}
@使用(Html.BeginForm())
{
电邮:
@Html.TextBoxFor(v=>v.Email)
密码:
@Html.PasswordFor(v=>v.Password)
}
}
其他的
{
激活链接已发送到您的电子邮件。
}
在注册视图中,更改
@using (Html.BeginForm())
到
在单控制器、单操作场景中,不需要额外的特定路由信息,但显然路由引擎无法自行确定使用多个控制器/操作路由到哪个控制器/操作
根据评论编辑:
这是一个路由问题。尝试为您的注册操作添加特定路由。差不多
routes.MapRoute(
"Register", // Route name
"{controller}/Index/{registrationResult}", // URL with parameters
new {
controller = "{controller}",
action = "Selector",
registrationResult= UrlParameter.Optional
}
);
“registrationResult”将是post操作中参数的名称。我认为视图模型非常相似,以至于路由引擎无法区分两者。将上述路由添加到默认路由之前,并在提交注册表时与之匹配。要解决我的问题,请从控制器上下文中检查IsChildAction属性。我还必须清除模型状态
[HttpPost]
public ActionResult LogIn(AuthViewModel authResult)
{
if (!this.ControllerContext.IsChildAction)
{
if (ModelState.IsValid)
{
userService.LogInUser(authResult.Login, authResult.Password, Request.UserHostAddress);
}
else
{
TempData["AuthMessage"] = GetValidationMessage();
}
string redirectUrl = "/";
if (Request.UrlReferrer != null)
{
redirectUrl = Request.UrlReferrer.AbsoluteUri.ToString();
}
return Redirect(redirectUrl);
}
ModelState.Clear();
return PartialView("AuthControl");
}
“注意,您不允许嵌套表单元素!”在我的情况下,表单是不嵌套的。您可以发布标记吗?注册页面是什么样子的?@SteveMallory,这是一个简单的表单,我发布了它。我添加了其他信息,但不幸的是,我得到了相同的结果。@你能加载表单并验证两个表单的动作属性吗?我想看到的另一件事是Global.asaxt中的registerOutes方法这里只有默认路由{controller}/{action}/{id}。注册表单和登录表单看起来像感谢您的编辑。不幸的是,它仍然不起作用。然而,我在Phil Haack的博客中发现了一个类似的问题。他建议不要(在评论中)把形式放在局部视图中。
[HttpPost]
public ActionResult LogIn(AuthViewModel authResult)
{
if (!this.ControllerContext.IsChildAction)
{
if (ModelState.IsValid)
{
userService.LogInUser(authResult.Login, authResult.Password, Request.UserHostAddress);
}
else
{
TempData["AuthMessage"] = GetValidationMessage();
}
string redirectUrl = "/";
if (Request.UrlReferrer != null)
{
redirectUrl = Request.UrlReferrer.AbsoluteUri.ToString();
}
return Redirect(redirectUrl);
}
ModelState.Clear();
return PartialView("AuthControl");
}