DisplayAttribute: cannot retrieve property 'Name' because localization failed

Viewed 802

InvalidOperationException: Cannot retrieve property 'Name' because localization failed. Type 'Common.SharedResources' is not public or does not contain a public static string property with the name 'Common_From'.

Hi folks, I have a 'Common' class library project containing a resource file (SharedResources.resx) and an associated class (ShareResources.cs) which is working as expected by localising strings and error messages for a separate 'Web' project, but I can't get the same resources to work with the Display attribute on a view model - requesting the view throws the exception shown above.

Every similar problem I've found online was solved by ensuring the class and resource file are public and the 'Custom Tool' is set to 'PublicResXFileCodeGenerator', however as can be seen below this is already the case, so I'm at a bit of a loss.

View Model (Web) - replacing the key constants with regular strings also throws the exception.

[Display(ResourceType = typeof(SharedResources), Name = ResourceKeys.Value.Common.From)]
public DateTime? FromDate { get; set; }

SharedResources.cs (Common)

public class SharedResources
    {
        // Intentionally blank.
    }

SharedResources.resx (Common)

enter image description here

SharedResources.Designer.cs

enter image description here

SharedResources.resx Properties

enter image description here

1 Answers

It seems to me we are missing something here.

Part 1: The Solution

This is your model, not your view (strictly speaking):

[Display(ResourceType = typeof(SharedResources), Name = ResourceKeys.Value.Common.From)]
public DateTime? FromDate { get; set; }

What is the actual code in your view?

I solved this issue after solving a probably too specific case, the error I was getting was pointing out to the back-end, but the problem was actually in the front-end.

I was getting this error:

Cannot retrieve property 'Name' because localization failed. Type 'MyProject.Web.Resources.Checkbook' is not public or does not contain a public static string property with the name 'EDIT_ORIGINAL_FILE_LABEL'.

After some changes in that file (see part 2), I noticed that I was setting some option value in the some drop-down list to 0:

var tempList = `<option value="${0}" name="${"name"}">${"- Select- "}</option>`;

I changed the field part value="${0}" to value="" and the error get gone, and the validation worked correctly. This was the fix.

Part 2: The Fake Error

I tried to fix my error by changing the name of the EDIT_ORIGINAL_FILE_LABEL back. I mean, I had changed it from that, EDIT_ORIGINAL_FILE_LABEL to EDIT_FILE_LABEL, but after doing that I had this error from the backend:

The model item passed into the dictionary is of type 'MyProject.Web.Models.Checkbook.CheckbookViewModel', but this dictionary requires a model item of type 'MyProject.Web.Models.Checkbook.OtherCheckbookViewModel'.

Which is a absolute fake and misleading error. The problem here was in the front-end, not in the model validation to the controller.

Nevertheless, I'm still clueless about why changing the name of that field throws any error.

Additional code

In case you may need it:

In the model

[
    Display(
        Name = "EDIT_FILE_LABEL",
        ResourceType = typeof(Resources.Checkbook))
]
[
    Required(
        ErrorMessageResourceName = "EDIT_FILE_VALIDATION_MESSAGE",
        ErrorMessageResourceType = typeof(Resources.Checkbook))
]
public string SelectedFile { get; set; }

In the view

<div class="col-12 col-md-6 mb-3">
    @Html.LabelFor(Model => Model.SelectedFile)
    @Html.DropDownListFor(Model => Model.SelectedFile, new SelectList(Model.FileList.Select(u => new { Value = u.file_id, Text = u.file_number }), "Value", "Text"), $"- {MyProject.Web.Resources.Global.SELECT} -", new { @class = "form-control form-select w-auto", @id = "FilesId" })
    @Html.ValidationMessageFor(model => model.SelectedFile, "", new { @class = "text-danger" })
</div>
Related