MVC3 客户端验证 - 仅显示“ * 必填”

发布于 2024-10-27 18:01:06 字数 118 浏览 1 评论 0原文

有没有一种快速方法可以为模型中的所有字段默认错误消息?

我希望验证返回文本:

“ * required ”

...但不想在每个字段上手动设置它。

谢谢保罗

Is there a quick way to default the error message for all your fields in a model?

I want the validation to return the text:

" * required "

...but dont want to manually set it on each field.

Thanks Paul

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

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

发布评论

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

评论(2

酸甜透明夹心 2024-11-03 18:01:06

您可以编写自定义必需属性,

[AttributeUsage(AttributeTargets.Field | AttributeTargets.Property,                                              AllowMultiple = true, Inherited = true)]
public sealed class AEMRequiredAttribute: ValidationAttribute
{
    private const string _defaultErrorMessage = "* required";    
    public AEMRequiredAttribute()
        : base(_defaultErrorMessage)
    {        }    
    public override string FormatErrorMessage(string name)
    {
        return String.Format(CultureInfo.CurrentUICulture, "* required", name);
    }    
    public override bool IsValid(object value)
    {
        if (value == null || String.IsNullOrWhiteSpace(value.ToString())) return false;
        else return true;
    }
}

调用此属性,如下所示:

public partial class AEMClass
    {
        [DisplayName("Dis1")]
        [AEMRequiredAttribute]
        public string ContractNo { get; set; }
    }

you can write your custom Required Attribute

[AttributeUsage(AttributeTargets.Field | AttributeTargets.Property,                                              AllowMultiple = true, Inherited = true)]
public sealed class AEMRequiredAttribute: ValidationAttribute
{
    private const string _defaultErrorMessage = "* required";    
    public AEMRequiredAttribute()
        : base(_defaultErrorMessage)
    {        }    
    public override string FormatErrorMessage(string name)
    {
        return String.Format(CultureInfo.CurrentUICulture, "* required", name);
    }    
    public override bool IsValid(object value)
    {
        if (value == null || String.IsNullOrWhiteSpace(value.ToString())) return false;
        else return true;
    }
}

call this attribute as below :

public partial class AEMClass
    {
        [DisplayName("Dis1")]
        [AEMRequiredAttribute]
        public string ContractNo { get; set; }
    }
自此以后,行同陌路 2024-11-03 18:01:06

您可以创建一个新的 HTML 帮助程序,然后调用基础 ValidationMessageValidationMessageFor 帮助程序来设置消息文本。

基于 ValidationMessageFor 的东西看起来像这样:

public static class HtmlHelperExtensions {
        public static IHtmlString ValidatorMessageWithMyTextFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression) {
            return htmlHelper.ValidationMessageFor<TModel, TProperty>(expression, "required *");
        }
}

你可以使用以下命令将其添加到你的视图中

@Html.ValidatorMessageWithMyTextFor(m=>m.MyModelPropertyToValidate)

当然,所有这些都在应用程序的视图端而不是模型端工作,所以这完全取决于你想要的位置嵌入消息。如果是模型方面,那么 AEM 的解决方案是一个不错的解决方案。

You could create a new HTML helper and then call into the underlying ValidationMessage or ValidationMessageFor helpers setting the message text as you do so.

Something based on ValidationMessageFor would look like this:

public static class HtmlHelperExtensions {
        public static IHtmlString ValidatorMessageWithMyTextFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression) {
            return htmlHelper.ValidationMessageFor<TModel, TProperty>(expression, "required *");
        }
}

And you can add that to your view using

@Html.ValidatorMessageWithMyTextFor(m=>m.MyModelPropertyToValidate)

Of course that all works from the view side of the app and not the model side so it all depends where you would like to embed the messages. If it's the model side then AEM's solution is a good one.

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