来自 asp.net 的背景,在向页面添加验证时,我非常欣赏“validationGroup”的概念。我一直在 mvc.net 中寻找相应的概念,但运气不佳。
这个概念在 mvc.net 中可用吗?如果没有,我还有什么选择?
不幸的是,它没有附带任何类似的东西。
不久前我在博客中介绍了一种解决方法。
ASP.NET MVC - 具有 2 个表单和 1 个视图的验证摘要
博客文章的要点:
namespace System.Web.Mvc
{
public static class HtmlExtensions
{
public static string ActionValidationSummary(this HtmlHelper html, string action)
{
string currentAction = html.ViewContext.RouteData.Values["action"].ToString();
if (currentAction.ToLower() == action.ToLower())
return html.ValidationSummary();
return string.Empty;
}
}
}
And
<h2>Register</h2>
<%= Html.ActionValidationSummary("Register") %>
<form method="post" id="register-form" action="<%= Html.AttributeEncode(Url.Action("Register")) %>">
... blah ...
</form>
<h2>User Login</h2>
<%= Html.ActionValidationSummary("LogIn") %>
<form method="post" id="login-form" action="<%= Html.AttributeEncode(Url.Action("LogIn")) %>">
... blah ...
</form>
HTHs,
Charles
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)