2017-06-04 38 views
-4

基本上我有一个网站,用户输入的图像标题,我想要在网站上显示它。该图像的标题。ASP.NET - C#MVC5

Atm 我保存到一个文件夹并显示所有图像但我不能显示标题。它只是当我没有出现<h2>@Html.DisplayFor(m => m.title)</h2>

这样的事情:但我想要的职位的标题。 https://i.gyazo.com/17828889116a77983f70fd8c8a4c2ebf.png

也许我需要将图像保存到数据库?请帮助我。

查看:

@foreach (var image in Model.Images) 
{ 
    <h2>@Html.DisplayFor(m => m.title)</h2> 
    <div class="row">  
     <div class="col-md-8 portfolio-item"> 
      <img class="portrait" src="@Url.Content(image)" alt="Hejsan" /> 
     </div> 
    </div> 

} 

型号

[Table("MemeImages")] 
public class UploadFileModel 
{ 
    [Key] 
    [DatabaseGenerated(System.ComponentModel.DataAnnotations.Schema.DatabaseGeneratedOption.Identity)] 
    public int id { get; set; } 

    public string location { get; set; } 

    public IEnumerable<string> Images { get; set; } 

    public int contentLength { get; set; } 
    public string contentType { get; set; } 

    public string userID { get; set; } 

    [Required] 
    [Display(Name = "Describe your post...")] 
    public string title { get; set; } 

    public void SavetoDatabase(UploadFileModel file) 
    { 
     ApplicationDbContext db = new ApplicationDbContext();      
     db.uploadedFiles.Add(file); 
     db.SaveChanges(); 
    } 
} 

控制器:

public class MemesController : Controller 
{ 

    private string memesDirectory = "~/Content/Images/Memes/"; 

    // GET: Memes/Upload 
    public ActionResult Upload() 
    { 
     var uploadFile = new UploadFileModel(); 
     return View(uploadFile); 
    } 

    // GET: Memes/Hot 
    public ActionResult Hot(UploadFileModel uploadFileModel) 
    { 
     //Select every image on the server memesdirectory and posts it 
     uploadFileModel.Images = Directory.EnumerateFiles(Server.MapPath(memesDirectory)).Select(fn => memesDirectory + Path.GetFileName(fn)); 
     return View(); 
    } 

    // GET: Memes/Trending 
    public ActionResult Trending() 
    { 
     return View(); 
    } 

    // GET: Memes/Fresh 
    public ActionResult Fresh() 
    { 
     return View(); 
    } 

    [HttpPost] 
    public ActionResult Upload(UploadFileModel uploadModel) 
    {   
     if(Request.Files.Count > 0) 
     { 
      var file = Request.Files[0]; 

      if(file != null && file.ContentLength > 0) 
      { 
       //saves image to the server 
       var fileName = Path.GetFileName(file.FileName); 
       var path = Path.Combine(Server.MapPath(memesDirectory), fileName); 
       file.SaveAs(path); 

       //saves image-related data to the database 
       uploadModel.userID = User.Identity.GetUserId(); 
       uploadModel.location = path; 
       uploadModel.contentType = file.ContentType; 
       uploadModel.contentLength = file.ContentLength; 


       //saves to the database 
       uploadModel.SavetoDatabase(uploadModel); 
      } 
     } 
     return RedirectToAction("Index", "Home"); 
    } 
} 

}

+1

你的标题是 - 没有什么比一些标签更多,表明毫不费力就在你身边想出一个meaningfull问题 – TomTom

+0

我这样做,并得到了3次。 ;) – xDDD

回答

0
uploadFileModel.Images = Directory.EnumerateFiles(Server.MapPath(memesDirectory)).Select(fn => memesDirectory + Path.GetFileName(fn)); 
    return View(); 

可能需要更改为:

// Add code here to get uploadFileModel from the databases 
    uploadFileModel.Images = Directory.EnumerateFiles(Server.MapPath(memesDirectory)).Select(fn => memesDirectory + Path.GetFileName(fn)); 
    uploadFileModel.title = "something you want"; // this line may not be necessary if getting it from the database did this for you already 

    return View(uploadFileModel); 
+0

标题在数据库中。我如何检索它们并为每个图像插入每个标题? – xDDD

+0

在新的ApplicationDbContext()。uploadedFiles'上的foreach循环可能是你想要开始的地方。 – mjwills