Asp.net mvc 如何在asp.net MVC 4和Razor视图中显示路径中的图像?
我有以下型号:Asp.net mvc 如何在asp.net MVC 4和Razor视图中显示路径中的图像?,asp.net-mvc,image,razor,Asp.net Mvc,Image,Razor,我有以下型号: public class Player { public String ImagePath { get { return "~/Content/img/sql_error.JPG"; } } 这是我的.cshtml文件: @model SoulMasters.Models.Game.Player @{ ViewBag.Title = "Game"; Layout = "~/V
public class Player
{
public String ImagePath
{
get
{
return "~/Content/img/sql_error.JPG";
}
}
这是我的.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>
此外,我还尝试了以下行,效果很好:
如何从路径显示该图像?
图像路径是否会根据设置而有所不同?还有其他想法吗
我现在不太明白razor语法是如何工作的。在你看来,试着这样做
~/Content/img/sql_error.JPG
~/Content/img/sql_error.JPG
Sample Image asd model.ImagePath;
您也可以尝试以下答案:
<img src="~/Content/img/sql_error.JPG" alt="Sample Image" width="300px" />
model.ImagePath)“style=”高度:200px;宽度:200px;"/>
@foreach(模型中的var m)
{
}
试试这个
<img src="~/Content/img/@Html.DisplayFor(model =>model.ImagePath)" style="height:200px;width:200px;"/>
(或)
model.ImagePath)“style=”高度:50px;宽度:100px;“/>
您好,我正在数据库中将Imagepath保存为:c://user/tom/images
,我无法在视图中检索它为什么?非常感谢您的回答:)有时这些一行代码很有用!:)尝试此
请不要发布代码转储,而是解释此代码解决问题的原因。
<img src="~/Content/img/@Html.DisplayFor(model =>model.ImagePath)" style="height:200px;width:200px;"/>
@foreach (var m in Model)
{
<img src="~/Images/@m.Url" style="overflow: hidden; position: relative; width:200px; height:200px;" />
}
<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;"/>