0

I am very new to ASP.NET MVC 4 and I am trying to get an application to use the text from resources files of different countries (Resource.en-us.resx, Resource.es-Es.resx)

I used to have a LocalResource folder that I was able to access from a Model like this :

using Concordia_CRM.LocalResource;

[Required]
[Display(Name = "SD_NombreEmpresa", ResourceType = typeof(Resource))]
public string NombreEmpresa { get; set; }

But then I also read some answers mentioning the App_LocalResources and I have managed to apply those solution to access the texts from a VIEW but now dont know how to change the model to access them.

I have read questions and answers:

How to use app_GlobalResource or app_LocalResource?

How does the App_LocalResources work with MVC?

But have still not found a single approach to access the texts of the Resource file from a VIEW and a MODEL wich is compact and standard

From the model I have tried this

using Concordia_CRM.App_LocalResources <-- but this is not recognized

I can type Resources.Resource. but nothing else is available there and I dont want to have .resx files inside each view

Community
  • 1
  • 1
Mauricio Gracia Gutierrez
  • 10,288
  • 6
  • 68
  • 99
  • I think this link may help you http://stackoverflow.com/questions/6603050/how-to-access-my-resources-from-a-razor-view#6603050 – Ankush Jain Jun 22 '15 at 16:51
  • @AnkushJain I have applied that already and the answer only mentions how to access them from the VIEW not the model – Mauricio Gracia Gutierrez Jun 22 '15 at 17:00
  • @MauricioGracia there is no difference how to access resources between views (CSHTML) and model (CS), so could you please clarify why the same code did not work for you in CS? – Alexei Levenkov Jun 22 '15 at 17:03
  • @AlexeiLevenkov I have updated the question with my specific problems – Mauricio Gracia Gutierrez Jun 22 '15 at 17:08
  • Do you have to use local/global resource folders (as you've read through linked answers it is not the easiest thing to do in MVC)? If you've followed steps outlined in http://stackoverflow.com/questions/6603050/how-to-access-my-resources-from-a-razor-view#6603050 and resources should be generated and visible to your code and views... – Alexei Levenkov Jun 22 '15 at 18:24
  • @Alexei in the provided link they only mention Views and nothing about models. I am looking for a guide o detail sample – Mauricio Gracia Gutierrez Jun 22 '15 at 18:51
  • @AlexeiLevenkov I started with an empty template, maybe I am missing some configuration or reference – Mauricio Gracia Gutierrez Jun 23 '15 at 01:23

3 Answers3

1

Open Resources.resx file, in the top there is a combo to select access modifier, change to public, and you will be able to access them via

Resources.STRING1

Make sure to have namespace section in your web.config

<pages>
  <namespaces>
    <add namespace="ProjectName.App_LocalResources" />
  </namespaces>
</pages>

In the model class you can access resources :

 public class model1
{
   [Required(ErrorMessage=App_LocalResources.Resource1.String1)]
    public string MyProp { get; set; }
}
0

Turns out that you just need to add this line to any model

using Resources;  //<<-- This line

namespace Concordia_CRM.Models
{
    public class SolicitudDemo
    {
        [Required]
        [Display(Name = "SD_NombreEmpresa", ResourceType = typeof(Resource))]
        public string NombreEmpresa { get; set; }

...
}
Mauricio Gracia Gutierrez
  • 10,288
  • 6
  • 68
  • 99
0

Folloing this view of point I add a separate project just for resource files and add the reference to web application project.

Then I'll use not only on my dataannotations, also works on my controllers and views without use imports or using statement:

@section scripts {
   <script type="text/javascript">

      $(function () {
         $("#frm").data("CorregirError", "@Resources.CorregirError");
      });

   </script>
}
Dark_V
  • 1
  • 1