0

I have started working on an MVC project and I came across some scenarios where I feel I am stuck. I need to convert the existing MVC3 site to work for multiple language.

I have one HeaderPage.cshtml and it has a view model bound to it by

@model IHeaderPage

And it outputs a property of this model:

<h3>@Model.HeaderName</h3>

I called this view from MainPage.cshtml

@Html.Partial("HeaderPage")

Now in the Controller's Action method I change the model's property

objHeaderPage.HeaderName="Fill your Registeration details";

And when i run the project i see the the text "Fill your Registeration details".

Now how can I change the text value, i.e. it should read from my resx file. I have already created resx files in App_LocalResources folder. I heard that, it can be done by Display Attribute.. but how do i do that or is there any other better way?

Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
thinkmmk
  • 487
  • 1
  • 8
  • 22

2 Answers2

1

This should answer your question regarding the use of DisplayAttribute.

I use DisplayAttribute for every property of my ViewModel, but if you have to handle custom messages like "The record can not be saved because of an error...", or something similar, you can simply use

 objHeaderPage.HeaderName = Resources.ResourceMessageName; 

The framework will choose automatically the correct culture. I prefer to put all my resources in a separate projects so I can deploy only the dll of the resources in case of need, but you can also think to deploy the resx files to edit them directly on the production machine. I guess it's up to what you prefer/need.

Community
  • 1
  • 1
Iridio
  • 9,213
  • 4
  • 49
  • 71
  • Thanks a lot, I didnt knew that we can do this in the code behind. I did as you said. Apart from this I also have one registeration form where in labels and their respective input fields are shown. I have used @Html.LabelFor(mdl=>mdl.Person.FullName) , this renders a label in english, so now how to get the value from the resource files in such scenarios? – thinkmmk Mar 15 '12 at 14:52
  • Two ways imo. Use DisplayAttribute directly in your Person's entity, or do some flattening in your viewmodel creating a property like string PersonName {get;set;}. I prefer the second one for complex projects. – Iridio Mar 15 '12 at 15:26
0

use System.ComponentModel.DataAnnotations Namespace in ViewModel.

[Display(Name="Fill your Registeration details")]
public string HeaderName{get;set;}

also you can use your resource file. Just review http://msdn.microsoft.com/en-us/library/system.componentmodel.dataannotations.aspx

Yorgo
  • 2,668
  • 1
  • 16
  • 24