yam*_*pog 4 validation asp.net-mvc
来自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;
}
}
}
Run Code Online (Sandbox Code Playgroud)
和
<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>
Run Code Online (Sandbox Code Playgroud)
HTHs,
查尔斯