如何翻译这个图像源到Url.Content? [英] How to Translate this Image Source into Url.Content?

查看:113
本文介绍了如何翻译这个图像源到Url.Content?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有渲染这种方式如下图所示。

  <img src="../../../..@Model.FloorPlan.Floor_Plan_Image_Path@Model.FloorPlan.Floor_Plan_Image_Filename" alt=""/>

我想如果可能的话它的src属性将变为Url.Content。

I want if possible it's src attribute will be changed into Url.Content.

我曾尝试是这样的,但我的问题是它把我的模型为字符串:

<img src="@Url.Content("~/Model.FloorPlan.Floor_Plan_Image_Path@Model.FloorPlan.Floor_Plan_Image_Filename")" alt=""/>

谁能帮我?

路径的价值和文件名如下:

Model.FloorPlan.Floor_Plan_Image_Path =/内容/上传/平面图/ 00004601 /
Model.FloorPlan.Floor_Plan_Image_Filename =testfloorplan.png

Model.FloorPlan.Floor_Plan_Image_Path = "/Content/Uploads/FloorPlans/00004601/" Model.FloorPlan.Floor_Plan_Image_Filename = "testfloorplan.png"

推荐答案

我发现自己在一个情况下,我需要格式化字符串几乎任何观点,
所以我做了这个一个扩展方法,只是为了摆脱那些的String.Format 的每一个观。

I found myself in a situation where I would need to format the string almost on any view, so I made an extension method for this, just to get rid of those String.Format's on every View.

public static class UrlHelpers
{
    public static string Content(this UrlHelper urlHelper,
                                 string formatPath, 
                                 params object[] args)
    {
        return urlHelper.Content(String.Format(formatPath, args));
    }
}

这只是简单地格式化指定的路径,没有什么事情,但称这将是一个有点更好看。

It just simply formats the specified path, nothing much going on, however calling it would be a bit nicer to read.

@{
    var path = Model.FloorPlan.Floor_Plan_Image_Path;
    var imageName = Model.FloorPlan.Floor_Plan_Image_Filename;
}
<img src="@Url.Content("~{0}{1}", path, imageName)"/>

这篇关于如何翻译这个图像源到Url.Content?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆