我只需要显示我插入的多张图片
I need just to display the multiple images I insert
我只需要显示我插入的多张图像,它会保存在路径中,但是当他显示时它确实显示了。我不知道我的问题出在哪里,但这是我的代码,请大家帮帮我?
我的控制器
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))" />
我怎样才能显示我的图片?
我看到您在循环中将文件保存在控制器中,但没有使用文件路径更新模型。使用控制器中的文件路径更新您的模型,并将模型传递给视图,就像您正在做的那样。然后,在您的视图中,您还必须执行一个循环,遍历图像并为每个图像渲染一个 img 标签,并将源作为保存在控制器中的文件路径。
假设您使用的是 Razor ASP.NET 语法,只需像在控制器中那样在您的视图中执行类似的操作。但请务必先将文件附加到您的模型:
@foreach (var item in Model.Files)
{
<img src="@item.FilePath" />
}
这是另一个 post Razor 中的循环示例,祝你好运!
MVC Razor @foreach
首先为文件创建一个列表
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" />
}
我只需要显示我插入的多张图像,它会保存在路径中,但是当他显示时它确实显示了。我不知道我的问题出在哪里,但这是我的代码,请大家帮帮我?
我的控制器
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))" />
我怎样才能显示我的图片?
我看到您在循环中将文件保存在控制器中,但没有使用文件路径更新模型。使用控制器中的文件路径更新您的模型,并将模型传递给视图,就像您正在做的那样。然后,在您的视图中,您还必须执行一个循环,遍历图像并为每个图像渲染一个 img 标签,并将源作为保存在控制器中的文件路径。
假设您使用的是 Razor ASP.NET 语法,只需像在控制器中那样在您的视图中执行类似的操作。但请务必先将文件附加到您的模型:
@foreach (var item in Model.Files)
{
<img src="@item.FilePath" />
}
这是另一个 post Razor 中的循环示例,祝你好运!
MVC Razor @foreach
首先为文件创建一个列表
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" />
}