Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. How to reference resource from Resources file

How to reference resource from Resources file

Scheduled Pinned Locked Moved C#
tutorialhelpquestionlearning
4 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B Offline
    B Offline
    Bootzilla33
    wrote on last edited by
    #1

    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
    
    Richard DeemingR 1 Reply Last reply
    0
    • B Bootzilla33

      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
      
      Richard DeemingR Offline
      Richard DeemingR Offline
      Richard Deeming
      wrote on last edited by
      #2

      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

      "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

      B 1 Reply Last reply
      0
      • Richard DeemingR Richard Deeming

        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

        B Offline
        B Offline
        Bootzilla33
        wrote on last edited by
        #3

        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 DeemingR 1 Reply Last reply
        0
        • B Bootzilla33

          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 DeemingR Offline
          Richard DeemingR Offline
          Richard Deeming
          wrote on last edited by
          #4

          As far as I can see from your question, the values in the TempDataKey class represent the key of the TempData item. The message constants are defined in the Constants class, which is why I replaced that constant with the value from the resource file. The TempData 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

          "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups