How to fixed Error Message resource type in MVC?

1k Views Asked by At

There is multi lingual application in that we are facing issue with Error Message Resource type.

Error message data annotation not working as per the current culture.

We have ENGLISH and Spanish Culture files.

MODEL:

public class MyModel
{
 [DataType(DataType.EmailAddress)]
        [Required(ErrorMessageResourceType = typeof(MyLang.Resource), ErrorMessageResourceName = "emailrequired")]
        [Display(Name = "Email", ResourceType = typeof(MyLang.Resource))]
        public string Email { get; set; }

}

enter image description here

When I changed culture using code level then all the title changing correctly but the Error Message not converting to right language.

CODE:

string cultureName="en"; // or es
Thread.CurrentThread.CurrentCulture = new CultureInfo(cultureName);
Thread.CurrentThread.CurrentUICulture = Thread.CurrentThread.CurrentCulture;


View

<div class="form-group row">
<div class="col-md-2">
    @Html.LabelFor(x => x.Email, new { @class = "form-control-label" })                                        
</div>
<div class="col-md-4">
    @Html.TextBoxFor(model => model.Email, new { @class = "form-control" })
    @Html.ValidationMessageFor(model => model.Email)
</div>
</div>
1

There are 1 best solutions below

0
On

Try to include the jquery validation script in the view.

@section Scripts {
    @Scripts.Render("~/bundles/jqueryval")
}