简体   繁体   English

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

[英]How to Display Validation Error Messages on an ASP.NET MVC Page?

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,直到现在。

MVC 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 Web表单

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 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. 基本上,您将ErrorMessageisValid参数设置为后面代码中的相关验证器。 The related ValidationSummary should display the error message. 相关的ValidationSummary应显示错误消息。

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

If using WebForms you might use a Label control and set the '.Text' property of it with your result. 如果使用WebForms,您可以使用Label控件并使用结果设置它的“.Text”属性。 Or a Panel control. 或Panel控件。 Or a UserControl specifically for outputting error messages (this is what I do) that you can add to your MasterPage. 或者UserControl专门用于输出您可以添加到MasterPage的错误消息(这就是我所做的)。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM