Why I get error when I try to display image? - asp.net-mvc

I work use asp.net mvc5 in my project.
I try to display Image stored in my Shared folder in the project:
<img src="#Url.Content(~/Views/Shared/logo.png)" alt="" />
But I get this error on the fly:
Compiler Error Message: CS1525: Invalid expression term '/'
Any idea why I get the error above?

#Url.Content helper needs string as a parameter so you should write like this:
<img src="#Url.Content("~/Views/Shared/logo.png")" alt="" />

or you can use single brackets
<img src='#Url.Content("~/Views/Shared/logo.png")' alt='' />

Related

Render attribute content on condition

I need to conditionally define the content of an HTML 5 data attribute:
<img #(String.IsNullOrEmpty(prd.Reference)
? String.Format("data-title='{0}'", prd.Name)
: String.Format("data-title='{0}({1})'", prd.Name, prd.Reference)) />
When I run this code it is rendered as follows (when reference is not null):
<img data-title="'Product"/>
Sometimes even more strange result ... The Reference is missing.
Does anyone knows what am I missing?
Try with Html.Raw:
<img #Html.Raw((String.IsNullOrEmpty(prd.Reference)
? String.Format("data-title='{0}'", prd.Name)
: String.Format("data-title='{0}({1})'", prd.Name, prd.Reference)) />

How to put a #Model string inside a html string

How do I put a #Model string inside a html string?
<img src="/img/users/#Model.Short.jpg.ashx?width=80&height=80" width="80" height="80" alt="" />
The #Model.Short is the string I need to have as a part if the img src. The problem I have is that it gives an error as it thinks .jpg.ashx is a part of the #Model.Short and doesn't know that it should print that as it is.
The only thing I can think of now is to do this in JavaScript, but it would make more sense to have it "hardcoded" in the html.
Use parentheses:
<img src="/img/users/#(Model.Short).jpg.ashx?width=80&height=80" ... />

MVC 3 - _Layout.cshtml. Add a logo to the site

I'm new to MVC 3, I'm trying to add an Image to the _Layout.cshtml file.
I tried
<img runat="server" id="img_logo" alt="Logo" src="Content/Images/mailworks.png" />
no success. The logo only appear on some views. on others views for some reason the image is supposed to be in some other location - found it using firebug.
Try this:
<img id="img_logo" alt="Logo" src="#Url.Content("~/Content/Images/mailworks.png")" />
Use this:
<img src="#Url.Content("~/Content/Images/mailworks.png")"...
I found another quick solution : just append '/' at the beginning of the src's path.

Grails 2.0: Use variable for image resource

I am passing a variable logo which contains the file name of an image file from my controller to the GSP and then I try to display the image like this:
<img src="${resource(dir:'images',file:"${logo}")}" alt="Logo" border="0" />
Even though the variable logo contains the correct value I get an Unclosed GSP expression error:
java.lang.RuntimeException: Error initializing GroovyPageView
at org.grails.plugin.resource.DevModeSanityFilter.doFilter(DevModeSanityFilter.groovy:26) ~[plugin-classes/:na]
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886) [na:1.6.0_26]
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908) [na:1.6.0_26]
at java.lang.Thread.run(Thread.java:662) [na:1.6.0_26]
Caused by: org.codehaus.groovy.grails.web.taglib.exceptions.GrailsTagException: Unclosed GSP expression
... 4 common frames omitted
Replacing ${logo} with the file name works.
What am I doing wrong?
Thanks a lot
Jonas
You are trying to embeed Expresion Language inside Expresion Language.
Replace:
<img src="${resource(dir:'images',file:"${logo}")}" alt="Logo" border="0" />
By
<img src="${resource(dir:'images',file:logo)}" alt="Logo" border="0" />
Inside EL you can refer to variables directly
Ernest is right that you shouldn't use a GString in this case. The actual error is that values aren't correctly quoted.
You could also do
<img src='${resource(dir:"images",file:"${logo}")}' alt="Logo" border="0" />
(notice the single-quotes and double-quotes, they are properly closed)

Using Url.Content with semi-relative URL

I am storing the location of images in my database in an MVC application...but i am only storing part of the location. For example:
/headers/image1.jpg
/headers/image2.jpg
The images are actually stored in the following folder:
~/content/images/headers/image1.jpg
~/content/images/headers/image1.jpg
In my view, I want to do something like this:
<img src="#Url.Content("~/content/images") + Model.ImageUrl" />
How can I do this?
Just do it!
<img src="#Url.Content("~/content/images" + Model.ImageUrl)" />
UPDATE:
As of ASP.NET MVC 4 it is valid to use the tilde URLs directly in HTML as the Razor View Engine will parse the URLs. Like this:
<img src="~/content/images/#Model.ImageUrl" />
You can write extension method which combine your ImageUrl with configured path to content directory.
<img src="#Model.ImageUrl.ToRelative()" alt="#Model.ImageAlt" />
PS
Remember about alt tag. ;)

Resources