使用代码优先方法中的Id,从数据库上传,保存和检索图像

从过去10天开始,我尝试了许多方法/示例/教程,这些方法/示例/教程在网上提供以解决我的问题。 但是,对于所有这些情况,我失败了。 我想上传特定/多个产品的图像,将它们保存在数据库中,并通过调用它们的ID在主页中显示它们 。 任何人都可以为我提供一步一步的示例/教程/链接请不要给出部分答案/建议 。 因为我已经厌倦了那些。

我刚刚解决了我的问题,这是解决方案:

这是我的模型类

 public class Picture { public int PictureId { get; set; } public IEnumerable Image { get; set; } public string Name { get; set; } public long Size { get; set; } public string Path { get; set; } } 

这是我的控制器

  [HttpPost] public void Upload() //Here just store 'Image' in a folder in Project Directory // name 'UplodedFiles' { foreach (string file in Request.Files) { var postedFile = Request.Files[file]; postedFile.SaveAs(Server.MapPath("~/UploadedFiles/") + Path.GetFileName(postedFile.FileName)); } } public ActionResult List() //I retrive Images List by using this Controller { var uploadedFiles = new List(); var files = Directory.GetFiles(Server.MapPath("~/UploadedFiles")); foreach(var file in files) { var fileInfo = new FileInfo(file); var picture = new Picture() { Name = Path.GetFileName(file) }; picture.Size = fileInfo.Length; picture.Path = ("~/UploadedFiles/") + Path.GetFileName(file); uploadedFiles.Add(picture); } return View(uploadedFiles); } 

这是我的“索引”视图

  @using(Html.BeginForm("Upload", "Picture", FormMethod.Post, new { enctype="multipart/form-data" })){ 
Select a file:
}

通过此“列表”视图,我显示了图像列表:

  @foreach (var file in Model) {  }
Name Size Preview
@file.Name @file.Size