【发布时间】:2017-06-04 13:04:59
【问题描述】:
基本上我有一个网站,用户输入带有标题的图像,我想在网站上显示它。那张图片的标题。
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");
}
}
}
【问题讨论】:
-
你的标题是 - 只不过是一些标签,表明你想出一个有意义的问题是零努力
-
我这样做了,它获得了 3 次浏览。 ;)