asp.net-mvc 如何在 asp.net MVC 4 和 Razor 视图中显示路径中的图像?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15986980/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to display an image from a path in asp.net MVC 4 and Razor view?
提问by Sas Gabriel
I have the following model:
我有以下模型:
public class Player
{
public String ImagePath
{
get
{
return "~/Content/img/sql_error.JPG";
}
}
And, this is my .cshtmlfile:
而且,这是我的.cshtml文件:
@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>
The result is simple text:
结果是简单的文本:
~/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?
如何从路径显示该图像?图像路径会因设置而异吗?还有其他想法吗?
I don't really get it now on how razor syntax works.
我现在不太明白 razor 语法是如何工作的。
回答by Adithya
In your View try like this
在您的视图中尝试这样
<img src= "@Url.Content(Model.ImagePath)" alt="Image" />
回答by DKR
you can also try with this answer :
你也可以试试这个答案:
<img src="~/Content/img/@Html.DisplayFor(model =>model.ImagePath)" style="height:200px;width:200px;"/>
回答by Parameswaran
Try this ,
尝试这个 ,
<img src= "@Url.Content(Model.ImagePath)" alt="Sample Image" style="height:50px;width:100px;"/>
(or)
<img src="~/Content/img/@Url.Content(model =>model.ImagePath)" style="height:50px;width:100px;"/>
回答by Debendra Dash
@foreach (var m in Model)
{
<img src="~/Images/@m.Url" style="overflow: hidden; position: relative; width:200px; height:200px;" />
}

