Asp.net-Mvc

如何在 asp.net MVC 4 和 Razor 視圖中顯示路徑中的圖像?

  • April 13, 2013

我有以下模型:

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

如何從路徑顯示該圖像?圖像路徑會根據設置而有所不同嗎?還有其他想法嗎?

我現在不太了解剃刀語法的工作原理。

在您的視圖中嘗試這樣

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

引用自:https://stackoverflow.com/questions/15986980