Home ASP.NET MVC localization of separated model
Reply: 1

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#
praguan Reply to 2018-02-14 08:50:32Z

Here we go:

In Model:

[Display(Name = "InputDate", ResourceType = typeof(Resources.Resource))]
[DataType(DataType.Date)]
public DateTime? DateStart { get; set; }
  1. Define ResourceType of the attribute so it looks for a resource

  2. 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.

  3. Set the culture of current thread in the controller.

Resource.Culture = CultureInfo.GetCultureInfo("YourLocal");

  1. Set the accessibility of the resource to public

  2. 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!

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.339169 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO