简体   繁体   中英

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.

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"

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM