0

我有以这种方式呈现的以下图像。

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

如果可能的话,我希望它的 src 属性将更改为 Url.Content。

我尝试过的是这个,但我的问题是将我的模型视为字符串:

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

谁能帮我?

Path 和 Filename 的值如下:

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

4

3 回答 3

6

我发现自己几乎需要在任何视图上格式化字符串,所以我为此做了一个扩展方法,只是为了摆脱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));
    }
}

它只是简单地格式化指定的路径,没什么大不了的,但是调用它会更好读一些。

@{
    var path = Model.FloorPlan.Floor_Plan_Image_Path;
    var imageName = Model.FloorPlan.Floor_Plan_Image_Filename;
}
<img src="@Url.Content("~{0}{1}", path, imageName)"/>
于 2013-04-18T10:36:44.823 回答
4

我想我明白你的目的。试试这个:

<img src="@Url.Content(String.Format("~{0}{1}", Model.FloorPlan.Floor_Plan_Image_Path, Model.FloorPlan.Floor_Plan_Image_Filename))" alt=""/>
于 2013-04-18T09:48:43.277 回答
3

尝试

<img src="@Url.Content("~/" + Model.FloorPlan.Floor_Plan_Image_Path + Model.FloorPlan.Floor_Plan_Image_Filename)" alt="" />
于 2013-04-18T09:59:09.917 回答