Note that there are some explanatory texts on larger screens.

plurals
  1. POHow can I map database 'code' values into full strings using ASP.NET MVC4 C#?
    primarykey
    data
    text
    <p>I've got a external database (reading rights only) with fields containing codes representing stringdata. Unfortunatetly, the codes and explanations are not digitalized. Instead, I've got a bunch of papers stating T=Toyota, M=Mercedes, F=Ferrari, etc..</p> <p>I'm looking for a way to convert these <strong>codes into full strings</strong> and keep things maintainable.</p> <pre><code>[Desired result] [Current result] -Regnumber -Brand -Regnumber -Brand ABC001 Toyota ABC001 T FGH783 Ferrari FGH783 F LKJ345 Mercedes LKJ345M M </code></pre> <p>I considered using enums, but that seems like a lot of work declaring (500+ values) so I was thinking using resource files (XML should be easy to maintain) but <strong>I'm open for suggestions</strong>.</p> <p>My plan was to create a seperate resource file for every table containing codes:</p> <pre><code>./Resources/Cars/BRAND.resx ./Resources/House/BUILDINGTYPE.resx ./Resources/House/COLOR.resx ./Resources/Flowers/NAME.resx </code></pre> <p>That way, it would be easy to locate and change the content in the future. But I would like to know what would be a good place (Model, View, other) to receive the values from the resource file?</p> <p>Furthermore, I tried to use <a href="http://msdn.microsoft.com/en-us/library/system.componentmodel.dataannotations.aspx" rel="nofollow">DataAnnotations</a> but <strong>I'm probably missing the point completely</strong>. I was under the impression that one could pass a database value to a resource file just by adding a data annotation of display type "ResourceType = foo" but then I get the following error: </p> <p>Cannot retrieve property 'Name' because localization failed. Type 'MyProject.Resources.Car.BRAND' is not public or does not contain a public static string property with the name 'registration number' </p> <p>It seems like the resource file is using the display name instead of the field value retrieved from the database (the access modifier IS set to public)</p> <hr> <p>Model</p> <pre><code> // ... [Key] public int ID { get; set; } [Display(Name="registration number")] public string registrationNr { get; set; } [Display(Name="Brand", ResourceType = typeof(Resources.Car.BRAND))] public string BrandCode { get; set; } //... </code></pre> <hr> <p>View</p> <pre><code>foreach(modelItem in Model.Car) { //... @Html.DisplayNameFor(modelItem =&gt; modelItem.BrandCode) @Html.DisplayFor(modelItem =&gt; modelItem.BrandCode) //... } </code></pre> <hr> <p>However, the solution of my problem doesn't have to use resource files, as long as I can map codes to values and maintain them I'd be a happy man. Thanks in advance!</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload