I want to display and change a image dynamically in the same view. So I have tried to use the code: Use MVC 3 ViewBag to dynamically change an image
and this: Pass image from controller and display in a view using ViewBag in ASP.NET MVC 3
but didn't work.
Here is my Index.cshtml
:
@using (Html.BeginForm("showFile", "Home", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
@Html.TextBox("ImageID")
<input type="submit" value="ok" class="submit" />
}
<img src="@ViewBag.Foto" alt="IMAGES" />
My HomeController.cs
:
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");
}