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)"/>
Try
<img src="@Url.Content("~/" + Model.FloorPlan.Floor_Plan_Image_Path + Model.FloorPlan.Floor_Plan_Image_Filename)" alt="" />
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=""/>