我可以使用mvc 4 razor上传图像文件。现在我想显示/查看这些图像。我正在使用图像的产品。所以,我想展示特定产品的图像。如何显示与该产品相关的图像?
这是我的模特:
namespace Online_Shopping_Management.Models
{
public class Product
{
[Key]
public int ProductId { set; get; }
public int? CategoryId { set; get; }
public virtual Category Category { set; get; }
public int? SubCategoryId { set; get; }
public virtual SubCategory SubCategory { set; get; }
public int? ModelId { set; get; }
public virtual Model Model { set; get; }
public string ProductName { set; get; }
public string Number { set; get; }
public string Color { set; get; }
public decimal StandardCost { set; get; }
public decimal ListPrice { set; get; }
public string Size { set; get; }
public double Weight { set; get; }
public DateTime ProductionDate { set; get; }
public DateTime ExperiedDate { set; get; }
public IEnumerable<HttpPostedFileBase> Files { get; set; }
}
}
这是我的控制器部分:
public ActionResult Create(Product product)
{
foreach (var file in product.Files)
{
if (file.ContentLength > 0)
{
var fileName = Path.GetFileName(file.FileName);
var path = Path.Combine(Server.MapPath("~/Content/images"), fileName);
file.SaveAs(path);
}
}
if (ModelState.IsValid)
{
db.Products.Add(product);
db.SaveChanges();
return RedirectToAction("Index");
}
ViewBag.CategoryId = new SelectList(db.Categorys, "CategoryId", "Name", product.CategoryId);
ViewBag.SubCategoryId = new SelectList(db.SubCategories, "SubCategoryId", "SubCategoryName", product.SubCategoryId);
ViewBag.ModelId = new SelectList(db.Models, "ModelId", "ModelName", product.ModelId);
return View(product);
}
这是我的观点部分:
@model IEnumerable<Online_Shopping_Management.Models.Category>
@{
ViewBag.Title = "Index";
}
<h2>Index</h2>
<p>
Select from @Model.Count() categories:
</p>
<ul>
@foreach (var categories in Model)
{
<li>@Html.ActionLink(categories.Name, "Browse", new { categories = categories.Name })</li>
}
</ul>
答案 0 :(得分:1)
前几天我也面临同样的问题。检查此链接。我也在那里回答: Upload,Save and Retrieve Image from database by using their Id in Code First Method