How to translate this image source in Url.Content?

I have the following image that displays in this way.

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

I want, if possible, the src attribute will be changed to Url.Content.

I tried this, but my problem is that it treats my model as a string:

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

Can anybody help me?

The value of the path and file name is as follows:

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

0
c # asp.net-mvc asp.net-mvc-3 razor
Apr 18 '13 at 9:29
source share
3 answers

I found myself in a situation where I needed to format a string on almost any view, so I made an extension method for this, just to get rid of those String.Format on each 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 formats the specified path, nothing special happens, but calling it would be a little 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)"/> 
+5
Apr 18 '13 at
source share

I think I understand what you're going for. Try the following:

 <img src="@Url.Content(String.Format("~{0}{1}", Model.FloorPlan.Floor_Plan_Image_Path, Model.FloorPlan.Floor_Plan_Image_Filename))" alt=""/> 
+3
Apr 18 '13 at 9:48 on
source share

Try

 <img src="@Url.Content("~/" + Model.FloorPlan.Floor_Plan_Image_Path + Model.FloorPlan.Floor_Plan_Image_Filename)" alt="" /> 
+3
Apr 18 '13 at 9:59 on
source share



All Articles