【发布时间】:2014-12-01 22:36:08
【问题描述】:
我想在同一视图中动态显示和更改图像。 所以我尝试使用代码:Use MVC 3 ViewBag to dynamically change an image
还有这个:Pass image from controller and display in a view using ViewBag in ASP.NET MVC 3
但没用。
这是我的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" />
我的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");
}
【问题讨论】:
-
Use MVC 3 ViewBag to dynamically change an image 的可能重复项,如果一切都失败,请尝试谷歌搜索
-
查看呈现的 HTML 并确保它是正确的。例如:
<img src="/images/profile/id.jpg" alt="" />还要检查图像是否存在于位置 src 中。
标签: c# asp.net asp.net-mvc-5