如何在ASP.NET MVC页面上显示验证错误消息?

时间:2022-10-16 22:23:03

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.

我是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 ??

}

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.

将可变结果输出到页面的最佳方法是什么,因为我依赖于内置的ASP:Validators,直到现在。

4 个解决方案

#1


9  

MVC

See for a good example...

看一个很好的例子......

ASP.NET MVC Html.ValidationSummary(true) does not display model errors

ASP.NET MVC Html.ValidationSummary(true)不显示模型错误

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.

基本上,您需要传播错误消息以及控制器中的视图出错。 ModelStateDictionary.AddModelError()将为您处理这两项任务。

You can then use ValidationExtensions.ValidationSummary() to display.

然后,您可以使用ValidationExtensions.ValidationSummary()来显示。

Webforms

You don't have to use a validator for this. Most people don't. A simple styled DIV should work well.

您不必为此使用验证器。大多数人没有。一个简单的风格DIV应该很好。

eg.

<div id="errorMessageDiv" runat="server"></div>

Notice the runat parameter.

注意runat参数。

Now in your code-behind you can try

现在在您的代码隐藏中,您可以尝试

errorMessageDiv.innerHTML = "some error message";

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.

基本上,您将ErrorMessage和isValid参数设置为后面代码中的相关验证器。相关的ValidationSummary应显示错误消息。

#2


0  

Just add an asp label control to the page and then set it's text property with the return vale.

只需在页面中添加一个asp标签控件,然后使用return vale设置它的text属性。

#3


0  

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.

如果使用WebForms,您可以使用Label控件并使用结果设置它的“.Text”属性。或Panel控件。或者UserControl专门用于输出您可以添加到MasterPage的错误消息(这就是我所做的)。

#4


0  

You can use validation summary or you can use a label control to display the error message

您可以使用验证摘要,也可以使用标签控件来显示错误消息

#1


9  

MVC

See for a good example...

看一个很好的例子......

ASP.NET MVC Html.ValidationSummary(true) does not display model errors

ASP.NET MVC Html.ValidationSummary(true)不显示模型错误

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.

基本上,您需要传播错误消息以及控制器中的视图出错。 ModelStateDictionary.AddModelError()将为您处理这两项任务。

You can then use ValidationExtensions.ValidationSummary() to display.

然后,您可以使用ValidationExtensions.ValidationSummary()来显示。

Webforms

You don't have to use a validator for this. Most people don't. A simple styled DIV should work well.

您不必为此使用验证器。大多数人没有。一个简单的风格DIV应该很好。

eg.

<div id="errorMessageDiv" runat="server"></div>

Notice the runat parameter.

注意runat参数。

Now in your code-behind you can try

现在在您的代码隐藏中,您可以尝试

errorMessageDiv.innerHTML = "some error message";

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.

基本上,您将ErrorMessage和isValid参数设置为后面代码中的相关验证器。相关的ValidationSummary应显示错误消息。

#2


0  

Just add an asp label control to the page and then set it's text property with the return vale.

只需在页面中添加一个asp标签控件,然后使用return vale设置它的text属性。

#3


0  

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.

如果使用WebForms,您可以使用Label控件并使用结果设置它的“.Text”属性。或Panel控件。或者UserControl专门用于输出您可以添加到MasterPage的错误消息(这就是我所做的)。

#4


0  

You can use validation summary or you can use a label control to display the error message

您可以使用验证摘要,也可以使用标签控件来显示错误消息