2017-06-04 68 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