67

我有以下模型:

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>

结果是简单的文本:

~/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" />

如何从路径显示该图像?图像路径会根据设置而有所不同吗?还有其他想法吗?

我现在不太了解剃刀语法的工作原理。

4

4 回答 4

140

在您的视图中尝试这样

  <img src= "@Url.Content(Model.ImagePath)" alt="Image" />
于 2013-04-13T11:27:51.560 回答
10

你也可以试试这个答案:

 <img src="~/Content/img/@Html.DisplayFor(model =>model.ImagePath)" style="height:200px;width:200px;"/>
于 2015-10-02T11:45:56.020 回答
3

尝试这个 ,

<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;"/>
于 2018-01-02T09:18:02.550 回答
0
 @foreach (var m in Model)
    {
      <img src="~/Images/@m.Url" style="overflow: hidden; position: relative; width:200px; height:200px;" />
    }
于 2016-11-26T06:22:26.227 回答