How to customize Html.ValidationMessageFor in ASP MVC

后端 未结 7 876
Happy的楠姐
Happy的楠姐 2020-12-05 07:46

Is it possible to customize the Html.ValidationMessageFor method so that it produces different HTML?

I want to do something similar to:

7条回答
  •  没有蜡笔的小新
    2020-12-05 07:54

    Yes, just use a metamodel for the field:

    [MetadataType(typeof(YourMetaData))]
    public partial class YOURCLASS
    {
        [Bind(Exclude = "objID")]
        public class YourMetaData
        {
            [Required(AllowEmptyStrings = false, ErrorMessage = "Please enter a name")]
            public object Name { get; set; }
        }
    }
    

    Change your message at the ErrorMessage field :) Hope this help :)

提交回复
热议问题