[英]How to Translate this Image Source into Url.Content?
I have the following image which is rendered this way. 我有以下以这种方式渲染的图像。
<img src="../../../..@Model.FloorPlan.Floor_Plan_Image_Path@Model.FloorPlan.Floor_Plan_Image_Filename" alt=""/>
I want if possible it's src attribute will be changed into Url.Content. 我希望如果可能,它将src属性更改为Url.Content。
What I have tried is this but my problem is it treats my model as string: 我尝试过的是这个,但是我的问题是它将模型当作字符串对待:
<img src="@Url.Content("~/Model.FloorPlan.Floor_Plan_Image_Path@Model.FloorPlan.Floor_Plan_Image_Filename")" alt=""/>
Can anyone help me? 谁能帮我?
The value of the Path and Filename is as follows: 路径和文件名的值如下:
Model.FloorPlan.Floor_Plan_Image_Path = "/Content/Uploads/FloorPlans/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”
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. 我发现自己处于几乎需要在任何视图上格式化字符串的情况,因此我为此做了一个扩展方法,以摆脱每个View上的那些String.Format
。
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)"/>
I think I understand what you're going for. 我想我知道你要干什么。 Try this: 尝试这个:
<img src="@Url.Content(String.Format("~{0}{1}", Model.FloorPlan.Floor_Plan_Image_Path, Model.FloorPlan.Floor_Plan_Image_Filename))" alt=""/>
尝试
<img src="@Url.Content("~/" + Model.FloorPlan.Floor_Plan_Image_Path + Model.FloorPlan.Floor_Plan_Image_Filename)" alt="" />
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.