Home ASP.NET MVC localization of separated model

# ASP.NET MVC localization of separated model

Andrew Vorobyov
1#
Andrew Vorobyov Published in 2018-02-14 08:11:14Z
 I try to localize properties of my model which is separated dll in my asp.net mvc project. I use DisplayAttribute for this: [Display(Name = "InputDate", ResourceType = typeof(Resources.Resource))] [DataType(DataType.Date)] public DateTime? DateStart { get; set; }  Resource files are created in model project and have public modifier. When I use model with this property in view I see InputDate instead of value of this resource key. I think I didn't take into account something related with model assembly. Thank you!
praguan
2#
 Here we go: In Model: [Display(Name = "InputDate", ResourceType = typeof(Resources.Resource))] [DataType(DataType.Date)] public DateTime? DateStart { get; set; }  Define ResourceType of the attribute so it looks for a resource Define Name of the attribute which is used for the key of resource, you can use nameof for strong typed support instead of hard coding the key. Set the culture of current thread in the controller. Resource.Culture = CultureInfo.GetCultureInfo("YourLocal"); Set the accessibility of the resource to public Display the label in cshtml like this @Html.DisplayNameFor(model => model.DateStart ) You have to define resource key in project resources (.resx) file with specific localized value. That is all!