加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

asp.net-mvc – 如何显示图像从路径在asp.net mvc 4和剃刀视图

发布时间:2020-12-15 19:36:21 所属栏目:asp.Net 来源:网络整理
导读:这是模型。 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"
这是模型。
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" />

和这个工作

如何从路径显示该图像?
模型图像路径可以根据设置而不同。
任何其他想法?

我现在不知道如何剃刀语法工作。

谢谢,
Gabriel Sas

解决方法

在你的视图尝试这样
<img src= "@Url.Content(Model.ImagePath)" alt="Image" />

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读