Asp.Net MVC - 更改错误类名称

发布于 2024-08-17 15:52:09 字数 169 浏览 9 评论 0原文

当我在 Asp.Net MVC 中进行字段验证时,它会生成以下错误 css 类:

  • input-validation-error
  • field-validation-error

有没有办法可以更改生成的错误类的名称?

我用xVal。

When I do field validation in Asp.Net MVC, it's generated the following error css classes :

  • input-validation-error
  • field-validation-error

Is there a way that I can change the names of the error classes that are generated?

I use xVal.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

手心的海 2024-08-24 15:52:09

我不了解 xVal,但就 ASP.NET MVC 方面而言,如果您查看 您将看到有一些表示这些值的静态只读字符串。

public static readonly string ValidationInputCssClassName = "input-validation-error";
public static readonly string ValidationMessageCssClassName = "field-validation-error";
public static readonly string ValidationSummaryCssClassName = "validation-summary-errors";

因此,您可以根据自己的喜好编辑源代码(这也可能修复 xVal),或者更改输出代码以某种方式使用不同的值。

如果 xVal 使用 jQuery 验证(我认为它确实...?),您可以更改“errorClass”选项的默认值。 我在这里的回答可以帮助你小路。

HTH,
查尔斯

I don't know about xVal but as far as the ASP.NET MVC side of things go, if you have a look at the source you'll see there are some static readonly strings that represent these values.

public static readonly string ValidationInputCssClassName = "input-validation-error";
public static readonly string ValidationMessageCssClassName = "field-validation-error";
public static readonly string ValidationSummaryCssClassName = "validation-summary-errors";

So either you edit the source to your liking (which may also fix xVal) or alter the outputting code to use different values somehow.

If xVal is using jQuery Validation (which I think it does...?) you could change it's default value for the 'errorClass' option. My answer here could help you along that path.

HTHs,
Charles

蓦然回首 2024-08-24 15:52:09

这就是我所做的:

private void ChangeValidationClassNames()
{
    var helper = new HtmlHelper(new ViewContext(), new ViewPage());
    SetPublicStaticReadonly("ValidationInputCssClassName", helper, "errInput");
    SetPublicStaticReadonly("ValidationMessageCssClassName", helper, "errMsg");
}

public void SetPublicStaticReadonly(string readonlyPropName, object instance, object value)
{            
    var field = instance.GetType().GetField(readonlyPropName, BindingFlags.Static | BindingFlags.Public);
    if (field == null)
        throw new NullReferenceException(string.Format("public static readonly field '{0}' not found in '{1}'", readonlyPropName, instance));
    field.SetValue(instance, value);
}

Here's what I did:

private void ChangeValidationClassNames()
{
    var helper = new HtmlHelper(new ViewContext(), new ViewPage());
    SetPublicStaticReadonly("ValidationInputCssClassName", helper, "errInput");
    SetPublicStaticReadonly("ValidationMessageCssClassName", helper, "errMsg");
}

public void SetPublicStaticReadonly(string readonlyPropName, object instance, object value)
{            
    var field = instance.GetType().GetField(readonlyPropName, BindingFlags.Static | BindingFlags.Public);
    if (field == null)
        throw new NullReferenceException(string.Format("public static readonly field '{0}' not found in '{1}'", readonlyPropName, instance));
    field.SetValue(instance, value);
}
方圜几里 2024-08-24 15:52:09

更清洁的解决方案可能是:

<script type="text/javascript">
    $(document).ready(function(){
        $('.input-validation-error').addClass('CustomErrorClass').removeClass('input-validation-error');
        $('.field-validation-error').addClass('CustomErrorClass').removeClass('field-validation-error');
    });
</script>

More cleaner solution may be:

<script type="text/javascript">
    $(document).ready(function(){
        $('.input-validation-error').addClass('CustomErrorClass').removeClass('input-validation-error');
        $('.field-validation-error').addClass('CustomErrorClass').removeClass('field-validation-error');
    });
</script>

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文