How to Translate this Image Source into Url.Conten

2019-06-10 07:35发布

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"

3条回答
Viruses.
2楼-- · 2019-06-10 07:58

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=""/>
查看更多
Rolldiameter
3楼-- · 2019-06-10 08:03

Try

<img src="@Url.Content("~/" + Model.FloorPlan.Floor_Plan_Image_Path + Model.FloorPlan.Floor_Plan_Image_Filename)" alt="" />
查看更多
成全新的幸福
4楼-- · 2019-06-10 08:14

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)"/>
查看更多
登录 后发表回答