Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8869943
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T17:36:03+00:00 2026-06-14T17:36:03+00:00

In my MVC application I have the ability to get the error message from

  • 0

In my MVC application I have the ability to get the error message from a text file instead of using the default error message. This works perfectly on the Required attribute (both Serverside and Clientside).

I now need to do the same with the Compare attribute, but I can’t figure out how to override the Compare attribute.

For reference, this is how I am doing it with the Required attribute (I would like similar code to this to work with the Compare attribute)…

Public Class RequiredFieldAttribute
    Inherits ValidationAttribute
    Implements IClientValidatable

    Private innerAttribute As New RequiredAttribute()
    Private errormessagecontrolid As String

    Public Sub New(ErrorMessageControlID As String)

        Me.errormessagecontrolid = ErrorMessageControlID

    End Sub

    Protected Overrides Function IsValid(value As Object, validationContext As ValidationContext) As ValidationResult

        If Not innerAttribute.IsValid(value) Then
            Return New ValidationResult(ErrorMsgs.Text(Me.errormessagecontrolid))
        End If

        Return ValidationResult.Success

    End Function

    Public Function GetClientValidationRules(metadata As ModelMetadata, context As ControllerContext) As IEnumerable(Of ModelClientValidationRule) Implements IClientValidatable.GetClientValidationRules

        Dim result = New List(Of ModelClientValidationRule)

        Dim rule = New ModelClientValidationRule() With {.ErrorMessage = ErrorMsgs.Text(Me.errormessagecontrolid), .ValidationType = "required"}

        result.Add(rule)

        Return result

    End Function

End Class

Above, ErrorMsgs.Text is the function that retieves the message from the text file. Against my model I then apply something like this…

<RequiredField("AccountDetailsPostcodeError")>
Public Property PostCode As String

The system then looks in the Text file for an entry called AccountDetailsPostcodeError.

How can I achieve the same with the Compare attribute. At the moment I have a hard coded error message like this…

    <Compare("WebPassword", ErrorMessage:="The password and confirmation do not match.")>
    Public Property ConfirmWebPassword As String

Edit: The suggested fix below may work in C#, but won’t work in VB.NET, hence my more complex requirement to override the Compare attribute. I just don’t know how to correctly override it.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T17:36:04+00:00Added an answer on June 14, 2026 at 5:36 pm

    Why use a text file for your translations and messages, .NET has build in options for translations. You can use resources. The advantage of using resources is that resources are type safe, they are checked as compile time. Where your textfile can become corrupt / missing.

    The following guide helps you with setting up resources in a Mvc project:

    Step one

    Edit the default assembly language:

    • (C#) Properties > Assembly information > Neutral Language
    • (VB) My Project > Assembly information > Neutral Language

    Set this language to your default language. (For this example I use English (United States))

    Step two

    Add a resource file to your project. Call this file Resource.resx. Open this file. Change the Access Modifier to Public and start adding resource strings. For example:
    Default English resources

    Step three

    Add for each other language you want to support another resource file but name them Resource.LANGUAGE.resx where LANGUAGE is replaced by the other culture name. For culture names you can check this url: http://msdn.microsoft.com/en-us/goglobal/bb896001.aspx

    Then fill the new resource file with the localized strings. For example:
    English Resources

    Step four

    Then you can in your Models use the default localization support of the attributes:

    For example:

    VB:

    Imports System.ComponentModel.DataAnnotations
    Public Class UserModel
        <Display(Name:="UserNameField", ResourceType:=GetType(My.Resources.Resource))>
        <Required(AllowEmptyStrings:=False, ErrorMessageResourceName:="RequiredUsername", ErrorMessageResourceType:=GetType(My.Resources.Resource))>
        Public Property UserName As String
    
        <Display(Name:="PasswordField", ResourceType:=GetType(My.Resources.Resource))>
        <MinLength(6, ErrorMessageResourceName:="PasswordLengthError", ErrorMessageResourceType:=GetType(My.Resources.Resource))>
        <Compare("PasswordAgain", ErrorMessageResourceName:="CompareError", ErrorMessageResourceType:=GetType(My.Resources.Resource))>
        <Required(AllowEmptyStrings:=False, ErrorMessageResourceName:="RequiredPassword", ErrorMessageResourceType:=GetType(My.Resources.Resource))>
        Public Property Password As String
    
        <Display(Name:="PasswordAgainField", ResourceType:=GetType(My.Resources.Resource))>
        <Required(AllowEmptyStrings:=False, ErrorMessageResourceName:="RequiredPasswordAgain", ErrorMessageResourceType:=GetType(My.Resources.Resource))>
        Public Property PasswordAgain As String
    End Class
    

    C#

    using System.ComponentModel.DataAnnotations;
    public class UserModel
    {
        [Display(Name = "UserNameField", ResourceType = typeof(My.Resources.Resource))]
        [Required(AllowEmptyStrings = False, ErrorMessageResourceName = "RequiredUsername", ErrorMessageResourceType = typeof(My.Resources.Resource))]
        public string UserName;
    
        [Display(Name = "PasswordField", ResourceType = typeof(My.Resources.Resource))]
        [MinLength(6, ErrorMessageResourceName = "PasswordLengthError", ErrorMessageResourceType = typeof(My.Resources.Resource))]
        [Compare("PasswordAgain", ErrorMessageResourceName = "CompareError", ErrorMessageResourceType = typeof(My.Resources.Resource))]
        [Required(AllowEmptyStrings = False, ErrorMessageResourceName = "RequiredPassword", ErrorMessageResourceType = typeof(My.Resources.Resource))]
        public string Password;
    
        [Display(Name = "PasswordAgainField", ResourceType = typeof(My.Resources.Resource))]
        [Required(AllowEmptyStrings = False, ErrorMessageResourceName = "RequiredPasswordAgain", ErrorMessageResourceType = typeof(My.Resources.Resource))]
        public string PasswordAgain;
    }
    

    For localization the attribute needs to know the name of the static property and the type of the static class where to get the property from (as seen above).

    Step five

    Then in your view use the @Html.ValidationSummary() to get all the error messages, or use

    • VB @Html.ValidationMessageFor(Function(model) model.Property)
    • C# @Html.ValidationMessageFor(m => m.Property)

    to get specific error messages.

    For the display attribute you can use:

    • VB @Html.DisplayNameFor(Function(model) model.Property)
    • C# @Html.DisplayNameFor(m => m.Property)

    Changing the language

    And last but not least you can change the language of your app instead of your neutral language defined in step one by editing the Web.config and changing the globalization tag like so:

    
    <?xml version="1.0" encoding="utf-8"?>
    <configuration>
      <system.web>
        <globalization uiCulture="nl" />
      </system.web>
    </configuration>
    

    If you want to change the language from code you should edit System.Threading.Thread.CurrentThread.CurrentUICulture for information about this I suggest google or another SO question.


    Example project

    For this question I quickly made an example project to provide an accurate answer. Project can be found here:
    MvcVBTest.V1.zip


    UPDATE

    If you don’t want to use Resources but a single text file you can use the same concept the resource framework uses. You need a class that has static properties you can reference.

    For this purpose I did the following things:

    1. I created a class called Resources (Resources.vb).
    2. In this class I added a sub class called Resource
    3. In the static constructor of this class I open resource.xml which I have mapped to an array of Resource
    4. This array is then converted to an Dictionary(Of String, String)
    5. I created an static get property for every item in the xml. And returned the right item from the Dictionary
    6. I changed the ResourceType parameter in the UserModel class

    And of course a little clean up. The old resources can be deleted and the globalization tag can be removed from the web.config.

    Now all the text can be found in resource.xml as key value pairs. To add another line, add it to the XML and create a property for it in the Resource class.

    Example project

    For this update I updated my example project:
    MvcVBTest.V2.zip

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In my MVC application I have a default route defined: routes.MapRoute( Default, // Route
I have an mvc application that I allow businesses to log in using https://storea.mydomain.com
Is it possible to have one .NET MVC application, and have it accessible from
I have an application in PHP that is a simplified MVC type structure. This
In my mvc application I have Admin area and have default controllers outside area.
In my MVC application I have a View Model that looks similar to this:
I am developing an ASP.NET MVC 3 application using C# and Razor. I have
In my asp.net mvc application I'm using nhibernate 3.2 as ORM. I have catalogue
In my ASP .NET MVC application i have a link that refreshes the preview
I have one question; In my ASP.NET MVC web application have to do certain

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.