asp.net-mvc – ASP .Net MVC 3:子动作和重定向
发布时间:2020-09-06 02:28:01 所属栏目:asp.Net 来源:互联网
导读:我需要在主页上显示注册表和登录表. 如果在这两个表单上验证失败,我需要在主页上显示正确的错误. 但如果没有错误,则必须将用户重定向到安全的仪表板. 为此,我在主页上使用子操作,如下所示: @Html.Action(Register, Membership) 如果存在任何错误,它可以按预
|
我需要在主页上显示注册表和登录表. 如果在这两个表单上验证失败,我需要在主页上显示正确的错误. 但如果没有错误,则必须将用户重定向到安全的仪表板. 为此,我在主页上使用子操作,如下所示: @Html.Action("Register","Membership")
如果存在任何错误,它可以按预期完美地工作,因为它能够使用具有验证状态信息的适当模型重新呈现局部视图. 但是如果没有错误,当它尝试重定向时,它会抛出一个错误,指出: Child actions are not allowed to perform redirect actions. 有没有办法解决?我确信有一种方法可以在主页上放置注册和登录表单.很可能我不知道,因为我是ASP .Net MVC的新手. 你能指点我在正确的方向吗? 解决方法一种方法是使用ajax表单作为登录和注册位,而不是在提交有效时返回RedirectResult,返回一些json,其中一些客户端脚本将注意并用于执行重定向为了你.这是一个简化的例子. 控制器: using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.ComponentModel.DataAnnotations;
using MvcApplication12.Models;
namespace MvcApplication12.Controllers
{
public class HomeController : Controller
{
public ActionResult Index()
{
return View();
}
public ActionResult Register()
{
return PartialView(new UserDetails());
}
[HttpPost]
public ActionResult Register(UserDetails details)
{
if (ModelState.IsValid)
{
return Json(new {redirect = true,url = Url.Action("Index","Dashboard")});
}
else
{
return PartialView(details);
}
}
}
}
主页’索引’视图: @{
ViewBag.Title = "Index";
}
<script src="@Url.Content("~/Scripts/jquery-1.5.1.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.unobtrusive-ajax.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>
<script type="text/javascript">
function checkForRedirect(data)
{
if (data.redirect && data.url)
{
location.href = data.url;
}
}
</script>
<p>Home page stuff.....</p>
<div id="RegistrationArea">
@Html.Action("Register")
</div>
<p> Home page stuff.....</p>
注册表’注册’局部视图: @model MvcApplication12.Models.UserDetails
<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>
@using (Ajax.BeginForm(new AjaxOptions()
{
HttpMethod = "POST",Url = Url.Action("Register","Home"),OnSuccess = "checkForRedirect(data)",UpdateTargetId = "RegistrationArea",InsertionMode = InsertionMode.Replace
}))
{
@Html.LabelFor(m => m.UserName)
@Html.EditorFor(m => m.UserName)
@Html.ValidationMessageFor(m => m.UserName)
@Html.LabelFor(m => m.Password)
@Html.EditorFor(m => m.Password)
@Html.ValidationMessageFor(m => m.Password)
<input type="submit" />
} (编辑:4S站长网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- asp.net-mvc-3 – 为什么@ Html.Label()删除一些字符
- asp.net – 如果我没有指定targetFramework =“4.0”会发生
- asp.net-mvc – 允许asp.net mvc 2控制器名称的URL中的连字
- asp.net-mvc-4 – 在哪里可以找到WebMatrix.WebData.WebSec
- asp.net-mvc – 在MVC ActionLink中使用Knockout绑定
- 如何在不使用MembershipProvider的情况下使用ASP.NET登录控
- asp.net – jquery getJson没有将任何值传递给控制器
- asp.net-mvc – 如何成功配置Common.Logging?
- asp.net-mvc – 如何在ASP.NET MVC部分视图中使用匿名列表作
- asp.net-mvc – 未在ELMAH中记录的错误
推荐文章
站长推荐
- 防止双击asp.net按钮
- 如何通过Asp.net WebAPI中的异常过滤器传递内容?
- asp.net-mvc – 在一个页面中以两种不同的形式使
- asp.net-mvc – ASP.NET MVC中的Windows Live ID
- asp.net-mvc – 如何设置AntiForgeryToken cooki
- asp.net-mvc-3 – Azure网站上的RavenDb – 访问
- asp.net – 启用Application Insights会使Web应用
- asp.net-mvc-4 – AngularJs,DropZone.Js,MVC4 –
- asp.net web.config加密解密方法
- asp.net – 菜单控件生成的js导致Web窗体中的Sys
热点阅读
