如何在 ASP.NET MVC 页面上显示验证错误消息?
我对 ASP.NET 和 C# 还很陌生,我花了一天时间学习 ASP.NET 会员提供程序的基础知识,我已经构建了所有验证器,但在页面上输出错误消息时遇到了困难。
private void LogCreateUserError(MembershipCreateStatus status, string username)
{
string reasonText = status.ToString();
switch (status)
{
case MembershipCreateStatus.DuplicateEmail:
case MembershipCreateStatus.DuplicateProviderUserKey:
case MembershipCreateStatus.DuplicateUserName:
reasonText = "The user details you entered are already registered.";
break;
case MembershipCreateStatus.InvalidAnswer:
case MembershipCreateStatus.InvalidEmail:
case MembershipCreateStatus.InvalidProviderUserKey:
case MembershipCreateStatus.InvalidQuestion:
case MembershipCreateStatus.InvalidUserName:
case MembershipCreateStatus.InvalidPassword:
reasonText = string.Format("The {0} provided was invalid.", status.ToString().Substring(7));
break;
default:
reasonText = "Due to an unknown problem, we were not able to register you at this time";
break;
}
//CODE TO WRITE reasonText TO THE HTML PAGE ??
}
到目前为止,我一直依赖内置的 ASP:Validators,因此将变量结果输出到页面上的最佳方法是什么。
I am pretty new to ASP.NET and C# I have spent the day learning the basics of the ASP.NET Membership provider I have built all my validator but are getting stuck at outputting my error message on the page.
private void LogCreateUserError(MembershipCreateStatus status, string username)
{
string reasonText = status.ToString();
switch (status)
{
case MembershipCreateStatus.DuplicateEmail:
case MembershipCreateStatus.DuplicateProviderUserKey:
case MembershipCreateStatus.DuplicateUserName:
reasonText = "The user details you entered are already registered.";
break;
case MembershipCreateStatus.InvalidAnswer:
case MembershipCreateStatus.InvalidEmail:
case MembershipCreateStatus.InvalidProviderUserKey:
case MembershipCreateStatus.InvalidQuestion:
case MembershipCreateStatus.InvalidUserName:
case MembershipCreateStatus.InvalidPassword:
reasonText = string.Format("The {0} provided was invalid.", status.ToString().Substring(7));
break;
default:
reasonText = "Due to an unknown problem, we were not able to register you at this time";
break;
}
//CODE TO WRITE reasonText TO THE HTML PAGE ??
}
What is the best way to output the varible result onto the page as I have relied upon the built in ASP:Validators until now.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
MVC
请参阅一个很好的示例...
ASP.NET MVC Html.ValidationSummary(true) 不显示模型错误
基本上,您需要传播错误消息以及存在的事实< /em> 从控制器看到的错误。 ModelStateDictionary.AddModelError() 将为您处理这两项任务。
然后,您可以使用 ValidationExtensions.ValidationSummary( ) 显示。
Webforms
您不必为此使用验证器。大多数人都没有。一个简单风格的 DIV 应该可以很好地工作。
例如。
请注意
runat
参数。现在在您的代码隐藏中,您可以尝试
如果您确实想使用验证器结账...
http://weblogs.asp.net/ashicmahtab/archive/2008/12/12/putting-messages-into-a-validationsummary -control-from-code.aspx
基本上,您将 ErrorMessage 和 isValid 参数设置为后面代码中的相关验证器。相关的 ValidationSummary 应显示错误消息。
MVC
See for a good example...
ASP.NET MVC Html.ValidationSummary(true) does not display model errors
Basically, you need to propagate the error message and also that fact that there is an error to your view from your controller. ModelStateDictionary.AddModelError() will take care of both of these tasks for you.
You can then use ValidationExtensions.ValidationSummary() to display.
Webforms
You don't have to use a validator for this. Most people don't. A simple styled DIV should work well.
eg.
Notice the
runat
parameter.Now in your code-behind you can try
If you really want to use a validator checkout...
http://weblogs.asp.net/ashicmahtab/archive/2008/12/12/putting-messages-into-a-validationsummary-control-from-code.aspx
Basically you set the ErrorMessage and isValid parameters to the related validator in the code behind. The related ValidationSummary should display the error message.
您可以使用验证摘要,也可以使用标签控件来显示错误消息
You can use validation summary or you can use a label control to display the error message
如果使用 WebForms,您可以使用 Label 控件并使用您的结果设置它的“.Text”属性。或者面板控件。或者专门用于输出错误消息的用户控件(这就是我所做的),您可以将其添加到母版页中。
If using WebForms you might use a Label control and set the '.Text' property of it with your result. Or a Panel control. Or a UserControl specifically for outputting error messages (this is what I do) that you can add to your MasterPage.