【发布时间】:2010-11-24 22:12:43
【问题描述】:
我有一个包含表单的局部视图,并且此局部视图存在于包含其他一些表单和 html 的视图中。
当我按下提交并且验证失败时,它会在 URL 而不是原始 URL 中显示此部分视图表单操作。
父视图“用户帐户”: - 登录局部视图 - 注册局部视图
页面打开时的原始网址为:/users/account
注册验证失败时的 URL 变为:/users/register
这是我的部分观点:
<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<PicGhost.Website.ViewModels.RegisterViewModel>" %>
<% using (Html.BeginForm("Register", "Users", FormMethod.Post)) {%>
<%: Html.ValidationSummary(true) %>
<fieldset>
<legend>Fields</legend>
<div class="editor-label">
<%: Html.LabelFor(model => model.UserName) %>
</div>
<div class="editor-field">
<%: Html.TextBoxFor(model => model.UserName) %>
<%: Html.ValidationMessageFor(model => model.UserName) %>
</div>
<div class="editor-label">
<%: Html.LabelFor(model => model.Email) %>
</div>
<div class="editor-field">
<%: Html.TextBoxFor(model => model.Email) %>
<%: Html.ValidationMessageFor(model => model.Email) %>
</div>
<div class="editor-label">
<%: Html.LabelFor(model => model.Password) %>
</div>
<div class="editor-field">
<%: Html.PasswordFor(model => model.Password)%>
<%: Html.ValidationMessageFor(model => model.Password) %>
</div>
<div class="editor-label">
<%: Html.LabelFor(model => model.ConfirmPassword) %>
</div>
<div class="editor-field">
<%: Html.PasswordFor(model => model.ConfirmPassword) %>
<%: Html.ValidationMessageFor(model => model.ConfirmPassword) %>
</div>
<p>
<input type="submit" value="Register" />
</p>
</fieldset>
<% } %>
并注册动作:
[HttpPost]
public ActionResult Register(RegisterViewModel model)
{
if (ModelState.IsValid)
{
IUser user = _factory.CreateUser(model.UserName, model.Email, model.Password);
UserRepository.Add(user);
return RedirectToAction("Index");
}
return View(model);
}
如何避免显示这个错误的 URL 并保留原始 URL?
原网址:
验证后网址:
【问题讨论】:
标签: c# asp.net-mvc-2 .net-4.0 partial-views