2013-04-13 5 views
44

私は次のモデルがあります:asp.net MVC 4とRazorビューのパスから画像を表示するには?

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 [email protected] 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" /> 

パスからそのイメージを表示するにはどうすればよいですか? イメージパスは設定によって異なる場合がありますか?他のアイデア?

剃刀の構文がどのように機能するかは実際には分かりません。あなたのビューで

+1

この '' –

答えて

101

この

<img src= "@Url.Content(Model.ImagePath)" alt="Image" /> 
+0

を試し、これを試してみてください期待どおりに動作します、ありがとう:) –

+0

@SasGabrielあなたは大歓迎です:) – Adithya

+0

こんにちは、私はImagePathをデータベースに保存しています: 'c:// user/tom/images'、私はなぜそれを取得することができないのですか? – stom

6

ようにしてみてください、あなたはまた、この答えを試すことができます。

<img src="~/Content/img/@Html.DisplayFor(model =>model.ImagePath)" style="height:200px;width:200px;"/> 
1
@foreach (var m in Model) 
    { 
     <img src="~/Images/@m.Url" style="overflow: hidden; position: relative; width:200px; height:200px;" /> 
    } 
+4

コードのダンプを投稿しないでください。むしろ、このコードがこの問題を解決する理由を説明してください。 – Rakete1111

1

<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;"/> 
関連する問題