Try our conversational search powered by Generative AI!

Model form validation localization errormessage



I am trying to show localized error messages. I dont want hardcoded messages in my model. I have removed code to keep it simple.

My model

    public class BankIdNorModel : PageViewModel
        public BankIdNorModel(LoginPage currentPage) : base(currentPage)

        [Required(ErrorMessage = "I want to get this text from my xml language files like this /formerror/ssnrequired")]
        public string Ssn { get; set; }


My view

@using (Html.BeginForm())

    @Html.EditorFor(m => m.Ssn, new { htmlAttributes = new { @class = "form-control" } })

I guess i have to use ErrorMessageResourceType and ErrorMessageResourceName but how do i do that?

Cheers Øyvind

Feb 24, 2016 14:18

You can use @Html.ValidationMessageFor, e.g @Html.ValidationMessageFor(m => m.Ssn, Html.Translate("/formerror/ssnrequired"))

Feb 24, 2016 15:10

The suggestion from Tim works, but the error messages are always rendered with <span class="field-validation-valid"></span> or <span class="field-validation-error"></span>. This was confusing for me at least. :)

Oct 10, 2018 10:31
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.