如何在asp.net MVC 4和Razor视图中显示路径中的图像? [英] How to display an image from a path in asp.net MVC 4 and Razor view?
本文介绍了如何在asp.net MVC 4和Razor视图中显示路径中的图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有以下型号:
public class Player
{
public String ImagePath
{
get
{
return "~/Content/img/sql_error.JPG";
}
}
而且,这是我的 .cshtml
file:
And, this is my .cshtml
file:
@model SoulMasters.Models.Game.Player
@{
ViewBag.Title = "Game";
Layout = "~/Views/Shared/_GameLayout.cshtml";
var imagePath = @Model.ImagePath;
}
<fieldset>
<legend>Player</legend>
<div class="display-field">
@Html.DisplayFor(model => model.ImagePath)
</div>
@imagePath
<div style="padding:10px;">
<img src=@imagePath alt="Sample Image" width="300px" />
<img alt="asd" >
model.ImagePath;
</img>
</div>
</fieldset>
结果是简单的文字:
~/Content/img/sql_error.JPG
~/Content/img/sql_error.JPG
Sample Image asd model.ImagePath;
此外,我尝试了以下一行,它有效:
Also, I have tried the following line, and it works:
<img src="~/Content/img/sql_error.JPG" alt="Sample Image" width="300px" />
如何从路径显示该图像?
图像路径可能因设置而异吗?还有其他想法吗?
How to display that image from path? Could the image path be different based on settings? Any other ideas?
我现在对razor语法的工作原理并不熟悉。
I don't really get it now on how razor syntax works.
推荐答案
在你的视图中尝试这样
<img src= "@Url.Content(Model.ImagePath)" alt="Image" />
这篇关于如何在asp.net MVC 4和Razor视图中显示路径中的图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文