title: Pass a simple string from controller to a view MVC3
body: <p>I know this seems pretty basic, and it should be, but I cant find out where I am going wrong. (I hve read other articles with similar titles on SO, and other resources on the web but still cant figure it out), so any help would be appreciated.</p>
  
  <p>I have a controller and in it I am setting a string variable. Now I don't mind if this takes the form of a property, an ActionResult, or a straight method. I just want a simple string that I can play with in the controller, and return it to the view.</p>
  
  <p>Essentially what I am trying to do is list the files in a given folder. So my logic is like this:</p>
  
  <ol>
  <li>Find the current folder (partially successful)</li>
  <li><p>Append the path to the where the files you want to located are. i.e. if my current folder is Web\ then I will append something like "Content\CSS" if I wanted to list all the CSS files for example. (I do this because I want to allow the user to dynamically change the site by selecting the css to apply). So it would look like:</p>
  
  <p>CurrentPath += "Content\CSS"</p></li>
  <li><p>I want load the file names into an array or list</p></li>
  <li>I want to pass this list to my view to render in a combo box (which is on my _Layout.cshtml).</li>
  </ol>
  
  <p>It is important to know that I am trying to view the string on the _Layout.cshtml as I cant just build another view for it. (Unless I am wrong, in that case I would appreicate any help).</p>
  
  <p>At the moment I am still working on getting a simple string passed to my view in a way I can freely manipulate it like in step 2.</p>
  
  <p>I started off with a separate static class and a global variable:</p>
  
  <pre><code>public static class MyTheme
  {
      public static string CurrentPath = HostingEnvironment.MapPath("~");
  }
  </code></pre>
  
  <p>In my view I had:
      @Html.Label(MyProject.Web.Controllers.MyTheme.CurrentPath);</p>
  
  <p>This worked but when I tried to use an if statement to determine if the string was null or empty I got errors. So my next attempts all failed.</p>
  
  <p>Next I decided to bring it into a controller (in this case my BaseController) and this is when I started running into problems. Code below:</p>
  
  <p>Inside BaseController Class</p>
  
  <pre><code>    public ActionResult ThemePath()
      {
          string currentPath = Server.MapPath("~");
  
          if (string.IsNullOrEmpty(currentPath))
          {
              currentPath = "Error!";
          }
          else
          {
              currentPath = "Our Path Is: " + currentPath;
          }
  
          return View(currentPath);
      }
  </code></pre>
  
  <p>I dont know how to access and run this from inside my _Layout.cshtml view</p>
  
  <p>So next I tried a standard method inside BaseController:</p>
  
  <pre><code>    public string ThemePath()
      {
          string currentPath = Server.MapPath("~");
  
          if (string.IsNullOrEmpty(currentPath))
          {
              currentPath = "Error!";
          }
          else
          {
              currentPath = "Our Path Is: " + currentPath;
          }
  
          return currentPath;
      }
  </code></pre>
  
  <p>Again I don't know how to access it in the view</p>
  
  <p>Finally I tried to use ViewBag and ViewData and now I am just going bonkers! So in my base controller I have:</p>
  
  <pre><code>    public string ThemePath()
      {
          ViewBag.currentPath = Server.MapPath("~");
  
          if (string.IsNullOrEmpty(ViewBag.currentPath))
          {
              ViewBag.currentPath = "Error!";
          }
          else
          {
              ViewBag.currentPath = "Our Path Is: " + ViewBag.currentPath;
          }
  
          return ViewBag.currentPath;
      }
  </code></pre>
  
  <p>and in my view I have</p>
  
  <pre><code>     @Html.Label(ViewBag.CurrentPath);
  </code></pre>
  
  <p>or even</p>
  
  <pre><code>     @Html.Label(ViewBag.CurrentPath.ToString());
  </code></pre>
  
  <p>With the following friendly little error messages:</p>
  
  <p>CS1973: 'System.Web.Mvc.HtmlHelper' has no applicable method named 'Label' but appears to have an extension method by that name. Extension methods cannot be dynamically dispatched. Consider casting the dynamic arguments or calling the extension method without the extension method syntax.</p>
  
  <p>Finally I tried ViewData in the base as follows:
          public string ThemePath()
          {
              ViewData["currentPath"] = Server.MapPath("~");</p>
  
  <pre><code>        if (string.IsNullOrEmpty(ViewData["currentPath)"].ToString()))
          {
              ViewData["currentPath"] = "Error!";
          }
          else
          {
              ViewData["currentPath"] = "Our Path Is: " + ViewData["currentPath"];
          }
  
          return ViewData["currentPath"].ToString();
      }
  </code></pre>
  
  <p>and correspondingly in the _Layout.cshtml I tried:</p>
  
  <pre><code>     @Html.Label(ViewData["CurrentPath"].ToString());
  </code></pre>
  
  <p>Without the .ToString() I get the above error:</p>
  
  <p>With the .ToString() I get a null refrence execption error.</p>
  
  <p>So I am kinda lost now and don't really know where I am going wrong. Thanks in advance for any help.</p>