asp.net-mvc - ASP MVC 5 图像显示

标签 asp.net-mvc image

在我的 Visual Studio 项目中,我有一个用于存储图像的文件夹。此图像的路径存储在 SQL Server 数据库中,我试图在 View 中显示此图像。从我目前所做的研究来看,需要创建一个 HTML 助手,我已经尝试过了。我如何将路径传递给助手,以便它呈现在我的 View 中,在内容显示下?

查看

@Html.DisplayFor(modelItem => item.contentDisplay)  

助手

public static class CustomHtmlHelper
{
public static IHtmlString Image(this HtmlHelper helper, string src)
    {
        TagBuilder tb = new TagBuilder("img");
        tb.Attributes.Add("src", VirtualPathUtility.ToAbsolute(src));
        return new MvcHtmlString(tb.ToString(TagRenderMode.SelfClosing));
    }
}

Controller

private dbmb17adtEntities db = new dbmb17adtEntities();

    // GET: tbl_Post2



    public ActionResult Index()
    {
        var tbl_Post = db.tbl_Post.Include(t => t.tbl_User);
        return View(tbl_Post);

型号

public partial class tbl_Post
{
    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
    public tbl_Post()
    {
        this.tbl_Comment = new HashSet<tbl_Comment>();
        this.tbl_Tag = new HashSet<tbl_Tag>();
    }

    public int Id { get; set; }
    public string title { get; set; }
    public string content { get; set; }
    public string contentDisplay { get; set; }
    public string tags { get; set; }
    public Nullable<System.DateTime> createTime { get; set; }
    public Nullable<System.DateTime> updateTime { get; set; }
    public Nullable<int> commentCount { get; set; }
    public int authorId { get; set; }
    public Nullable<int> likeCount { get; set; }

我如何使用我认为的助手(如果确实正确)?

最佳答案

你可以试试

@foreach (var items in tbl_Post)
{
  <img src="@Url.Content(items.contentDisplay)" alt="" />
}

关于asp.net-mvc - ASP MVC 5 图像显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47393711/

相关文章:

c# - 如何在 mvc c# 中的 jquery 中为 ViewBag 赋值

c++ - 将图片从excel复制到剪贴板->在程序中输出->得到平滑的图片。失败在哪里?

html - 在列框的中间对齐图像

ios - 在 Collection View 单元格中使用淡入淡出效果动画图像

asp.net-mvc - 如何使用共享主机在我的网站中添加 ssl?

.net - 为什么 FormsAuthentication.RedirectFromLoginPage() 以 context.Response.Redirect(..., false) 结尾?

asp.net - 自定义 asp.net webforms 标记

asp.net-mvc - 包含文件的 ASP.NET MVC 模型

c# - silverlight 可以支持的最大分辨率图像文件是多少?

image - 如何打印处理中的图像