如何在ASP.NET MVC页面上显示验证错误消息? [英] How to Display Validation Error Messages on an ASP.NET MVC Page?

查看:198
本文介绍了如何在ASP.NET MVC页面上显示验证错误消息?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对ASP.NET和C#来说很新鲜我已经花了一天时间学习ASP.NET Membership Provider的基础知识我已经构建了所有的验证器,但是在页面上输出我的错误消息时遇到困难。

  private void LogCreateUserError(MembershipCreateStatus状态,字符串用户名)
{
字符串reasonText = status.ToString( );

switch(状态)
{
案例MembershipCreateStatus.DuplicateEmail:
案例MembershipCreateStatus.DuplicateProviderUserKey:
案例MembershipCreateStatus.DuplicateUserName:

reasonText =您输入的用户信息已经注册。;
休息;

案例MembershipCreateStatus.InvalidAnswer:
案例MembershipCreateStatus.InvalidEmail:
案例MembershipCreateStatus.InvalidProviderUserKey:
案例MembershipCreateStatus.InvalidQuestion:
案例MembershipCreateStatus.InvalidUserName:
case MembershipCreateStatus.InvalidPassword:

reasonText = string.Format(The {0} provided is invalid。,status.ToString()。Substring(7));
休息;
默认值:
reasonText =由于未知的问题,我们目前无法注册您;
休息;

}

//代码将理由文本写入HTML页面??


$ / code>

将可变结果输出到页面,因为我依赖内置的ASP:Validators直到现在。

解决方案

MVC

一个很好的例子...



基本上,您需要传播错误消息 ModelStateDictionary.AddModelError()将照顾这两项任务。



然后,您可以使用 ValidationExtensions.ValidationSummary()

Webforms



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



eg。

 < ; div id =errorMessageDivrunat =server>< / div> 

请注意 runat 参数。



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

  errorMessageDiv.innerHTML =some error message; 

如果您真的想使用验证器结帐...



http://weblogs.asp.net/ashicmahtab/archive/2008/12/12/putting-messages-into-a-validationsummary-control-from-code.aspx



基本上你在后面的代码中设置了 ErrorMessage isValid 参数相关的验证器。相关的 ValidationSummary 应显示错误消息。


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.

解决方案

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.

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

Notice the runat parameter.

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 the related validator in the code behind. The related ValidationSummary should display the error message.

这篇关于如何在ASP.NET MVC页面上显示验证错误消息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆