Sas*_*iel 58 asp.net-mvc image razor
我有以下型号:
public class Player
{
public String ImagePath
{
get
{
return "~/Content/img/sql_error.JPG";
}
}
Run Code Online (Sandbox Code Playgroud)
而且,这是我的.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>
Run Code Online (Sandbox Code Playgroud)
结果是简单的文字:
~/Content/img/sql_error.JPG
~/Content/img/sql_error.JPG
Sample Image asd model.ImagePath;
Run Code Online (Sandbox Code Playgroud)
此外,我尝试了以下行,它的工作原理:
<img src="~/Content/img/sql_error.JPG" alt="Sample Image" width="300px" />
Run Code Online (Sandbox Code Playgroud)
如何从路径显示该图像?根据设置,图像路径可能不同吗?还有其他想法吗?
关于剃刀语法是如何工作的,我现在还没有真正理解它.
小智 124
在你的视图中尝试这样
<img src= "@Url.Content(Model.ImagePath)" alt="Image" />
Run Code Online (Sandbox Code Playgroud)
你也可以尝试这个答案:
<img src="~/Content/img/@Html.DisplayFor(model =>model.ImagePath)" style="height:200px;width:200px;"/>
Run Code Online (Sandbox Code Playgroud)