我只需要显示插入的多张图像,将其保存在路径中,但是当他显示时确实会显示出来。我不知道我的问题在哪里,但这是我的代码,请大家帮我?
我的控制器
public async Task<ActionResult> Create(Inbox model, IEnumerable<HttpPostedFileBase> files)
{
var currentUser = await manager.FindByIdAsync(User.Identity.GetUserId());
if (ModelState.IsValid)
{
model.User = currentUser;
foreach (var file in files)
{
if (file != null && file.ContentLength > 0)
{
var fileName = Path.GetFileName(file.FileName);
var path = Path.Combine(Server.MapPath("~/FilesAPP"), fileName);
file.SaveAs(path);
path = Url.Content(Path.Combine("~/FilesAPP", fileName));
}
}
db.Inboxs.Add(model);
db.SaveChanges();
string url = Url.Action("List");
return Json(new { success = true, url = url });
}
return View(model);
}
我的模特
public string Files { get; set; }
我的观点
<img width="200" height="150" src="@Url.Content(Model.Files))" />
如何显示我的图像人?
答案 0 :(得分:0)
我看到您正在循环将文件保存在控制器中,但没有使用文件路径更新模型。使用Controller中的文件路径更新模型,然后像执行操作一样将模型传递给视图。然后,在您的视图中,还必须进行循环,遍历图像,并为每个图像渲染一个img标签,并将源作为文件路径保存在控制器中。
假设您使用的是Razor ASP.NET语法,则只需在视图中像在控制器中一样执行类似的操作即可。但请确保先将文件附加到模型:
@foreach (var item in Model.Files)
{
<img src="@item.FilePath" />
}
这是另一篇有关Razor中的循环示例的帖子,祝您好运!
答案 1 :(得分:0)
首先创建文件列表
public async Task<ActionResult> Create(Inbox model, IEnumerable<HttpPostedFileBase> files)
{
List<Inbox>lst=new List<Inbox>();
var currentUser = await manager.FindByIdAsync(User.Identity.GetUserId());
if (ModelState.IsValid)
{
model.User = currentUser;
foreach (var file in files)
{
if (file != null && file.ContentLength > 0)
{
var fileName = Path.GetFileName(file.FileName);
var path = Path.Combine(Server.MapPath("~/FilesAPP"), fileName);
file.SaveAs(path);
path = Url.Content(Path.Combine("~/FilesAPP", fileName));
lst.add(new Inbox{Files=path});
//you files added here
}
}
db.Inboxs.Add(model);
db.SaveChanges();
string url = Url.Action("List");
return Json(new { success = true, url = url });
}
return View(model);
}
您认为之后
@model List<Inbox>
@foreach (var item in Model.Files)
{
<img src="@item.FilePath" />
}