How to reference resource from Resources file
-
I have a bunch of messages in a Constants file that I want to convert all string constants for messages displayed to users to be moved to a localized/globalization resource file. I created a Resources.resx file and I'm trying to figure out how to reference a resource in a controller. For example this resource in my Resources.resx file
internal static string ExamItemResponseAddSuccessfully {
get {
return ResourceManager.GetString("ExamItemResponseAddSuccessfully", resourceCulture);
}
}I want to reference it in a controller POST action to replace the bolded reference to the string constant in the method below. How do I reference the resource above in the method below.
public async Task CreateDorItemResponse(CreateEditDorItemResponseViewModel dorToCreateItemResponse, CancellationToken cancellationToken)
{
// Create a new Dor Item PacketResponse to receive the required properties from the view model
var newDorItemResponse = new DorItemResponse();// Attempt to transfer the properties from the view model into the Dor if (ModelState.IsValid) { try { //Get the item to return back to Edit Item page. var item = await \_dorService.GetItemAsync(dorToCreateItemResponse.ItemId, cancellationToken) .ConfigureAwait(true); // Save the Dor Item Response \_mapper.Map(dorToCreateItemResponse, newDorItemResponse); newDorItemResponse.CreatedBy = User.Identity.Name; newDorItemResponse.ModifiedBy = User.Identity.Name; await \_dorService.CreateEditItemResponse(newDorItemResponse, User.Identity.Name, cancellationToken).ConfigureAwait(true); **TempData.Put(TempDataKey.Dor.RESPONSE\_ADD\_MESSAGE, StatusMessageModel.Create(Constants.Dor.RESPONSE\_ADD\_SUCCESS, false));** return RedirectToAction(nameof(EditDorItem), new {id = item.ItemId, dorId = item.DorId}); } catch (Exception) { // Save failed, add an error message and reload the same page TempData.Put(TempDataKey.Dor.RESPONSE\_ADD\_MESSAGE, StatusMessageModel.Create(Constants.Dor.RESPONSE\_UPDATE\_FAIL)); } } // Validation failed, reload the same page
-
I have a bunch of messages in a Constants file that I want to convert all string constants for messages displayed to users to be moved to a localized/globalization resource file. I created a Resources.resx file and I'm trying to figure out how to reference a resource in a controller. For example this resource in my Resources.resx file
internal static string ExamItemResponseAddSuccessfully {
get {
return ResourceManager.GetString("ExamItemResponseAddSuccessfully", resourceCulture);
}
}I want to reference it in a controller POST action to replace the bolded reference to the string constant in the method below. How do I reference the resource above in the method below.
public async Task CreateDorItemResponse(CreateEditDorItemResponseViewModel dorToCreateItemResponse, CancellationToken cancellationToken)
{
// Create a new Dor Item PacketResponse to receive the required properties from the view model
var newDorItemResponse = new DorItemResponse();// Attempt to transfer the properties from the view model into the Dor if (ModelState.IsValid) { try { //Get the item to return back to Edit Item page. var item = await \_dorService.GetItemAsync(dorToCreateItemResponse.ItemId, cancellationToken) .ConfigureAwait(true); // Save the Dor Item Response \_mapper.Map(dorToCreateItemResponse, newDorItemResponse); newDorItemResponse.CreatedBy = User.Identity.Name; newDorItemResponse.ModifiedBy = User.Identity.Name; await \_dorService.CreateEditItemResponse(newDorItemResponse, User.Identity.Name, cancellationToken).ConfigureAwait(true); **TempData.Put(TempDataKey.Dor.RESPONSE\_ADD\_MESSAGE, StatusMessageModel.Create(Constants.Dor.RESPONSE\_ADD\_SUCCESS, false));** return RedirectToAction(nameof(EditDorItem), new {id = item.ItemId, dorId = item.DorId}); } catch (Exception) { // Save failed, add an error message and reload the same page TempData.Put(TempDataKey.Dor.RESPONSE\_ADD\_MESSAGE, StatusMessageModel.Create(Constants.Dor.RESPONSE\_UPDATE\_FAIL)); } } // Validation failed, reload the same page
Something like this should work:
TempData.Put(TempDataKey.Dor.RESPONSE_ADD_MESSAGE, StatusMessageModel.Create(Properties.Resources.ExamItemResponseAddSuccessfully, false));
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
-
Something like this should work:
TempData.Put(TempDataKey.Dor.RESPONSE_ADD_MESSAGE, StatusMessageModel.Create(Properties.Resources.ExamItemResponseAddSuccessfully, false));
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
Richard Deeming wrote:
TempData.Put(TempDataKey.Dor.RESPONSE_ADD_MESSAGE, StatusMessageModel.Create(Properties.Resources.ExamItemResponseAddSuccessfully, false));
I'm trying to replace the TempData.Put(TempDataKey...) stuff because I that is in the Constants file and I'm getting rid of that altogether. I want to have the message come from the Resources file.
-
Richard Deeming wrote:
TempData.Put(TempDataKey.Dor.RESPONSE_ADD_MESSAGE, StatusMessageModel.Create(Properties.Resources.ExamItemResponseAddSuccessfully, false));
I'm trying to replace the TempData.Put(TempDataKey...) stuff because I that is in the Constants file and I'm getting rid of that altogether. I want to have the message come from the Resources file.
As far as I can see from your question, the values in the
TempDataKey
class represent the key of theTempData
item. The message constants are defined in theConstants
class, which is why I replaced that constant with the value from the resource file. TheTempData
key will never be shown to the user.
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer