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 に答える
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 に答える