title: Display image dynamically with ViewBag
body: <p>I want to display and change a image dynamically in the same view.
  So I have tried to use the code: <a href="https://stackoverflow.com/questions/14213182/use-mvc-3-viewbag-to-dynamically-change-an-image">Use MVC 3 ViewBag to dynamically change an image</a></p>
  
  <p>and this: <a href="https://stackoverflow.com/questions/16142526/pass-image-from-controller-and-display-in-a-view-using-viewbag-in-asp-net-mvc-3">Pass image from controller and display in a view using ViewBag in ASP.NET MVC 3</a></p>
  
  <p>but didn't work.</p>
  
  <p>Here is my <code>Index.cshtml</code>:</p>
  
  <pre><code>@using (Html.BeginForm("showFile", "Home", FormMethod.Post, new { enctype = "multipart/form-data" }))
  {
  
      @Html.TextBox("ImageID")
      &lt;input type="submit" value="ok" class="submit" /&gt;
  }
  
  &lt;img src="@ViewBag.Foto" alt="IMAGES" /&gt;
  </code></pre>
  
  <p>My <code>HomeController.cs</code>:</p>
  
  <pre><code>public ActionResult Index()
  {
      return View();
  }
  
  public ActionResult showFile(string ImageID)
  {
      var dir = Server.MapPath("/images/profile");
      var path = Path.Combine(dir, ImageID + ".jpg");
      if (System.IO.File.Exists(path))
      {
          ViewBag.Foto = path.ToString();
      }
      return RedirectToAction("Index", "Home");
  }
  </code></pre>