Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>First idea that comes to my mind is to save resource file's name in question into viewdata (or Session) and use a helper to get the value.</p> <p>Say you have two partners: Foo Logistics and Bar Solutions. Have a resource file for each of them: PartnerFoo.resx and PartnerBar.resx.</p> <p>In your controller, store the resource file you want to use into ViewData as in: </p> <pre><code>public ActionResult About() { ... ViewData["Resource"] = "MyMVCAppNamespace.Resources.PartnerFoo"; return View(); } </code></pre> <p>Include the namespace into the string too.</p> <p>Then code in the helper to retrieve the resource with viewdata.</p> <p>Helpers/Helper.cs:</p> <pre><code>namespace MyMVCAppNamespace.MvcHtmlHelpers { public static class HtmlHelpersExtensions { public static ResourceManager PartnerResource(this HtmlHelper helper) { // Get resource filename from viewdata string res = helper.ViewContext.ViewData["Resource"].ToString(); // Load the resource from assembly ResourceManager resourceManager = new ResourceManager(res, Assembly.GetExecutingAssembly()); return resourceManager; } } } </code></pre> <p>Now in the view, we are gonna use this helper to retrieve the string we want to write:</p> <p>About.cshtml:</p> <pre><code> @Html.PartnerResource().GetString("PartnerName") @Html.PartnerResource().GetString("PartnerCustomerServiceEmail") </code></pre> <p>Which gets rendered as:</p> <pre><code>Foo Logistics service@foologistics.com </code></pre> <p>or with PartnerBar</p> <pre><code>Bar Solutions service@barsolutions.com </code></pre> <p>We determine the resource file to use before the view is loaded. Then in view it gets dynamically rendered according to what resource is stored in to the viewdata. You can even store the resource filename into web.config and load the string in helper from there if you want.</p> <p>What's even more cool is that if you have localized resx file, say PartnerFoo.fi.resx and then use different culture (fi-FI in this case), the resourcemanager automatically looks up the localized version.</p> <p>You can even do simple branding by storing image URLs and whatnot in the resource file.</p> <p>It's simple really, but I hope it gets you started.</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.
    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