在我的 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/