Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to end code inside a string?

Tags:

c#

I know this is basic, but I'm feeling there's got to be a better way to do this.

I have a dynamic file name:

/img/car-1.jpg
/img/car-2.jpg
/img/car-3.jpg

In my Model View, I want to do this:

<img src="/img/[email protected]" />

Unfortunately the compiler considers the .jpg to be part of the code, and says 'int' does not contain a definition for jpg.

I also tried this:

<img src="/img/car-@{Model.Number}.jpg" />

But it gives me this error: 'System.Web.Mvc.WebViewPafe<TModel>.Model' is a 'property' but is used like a 'type'.

So for now I use a string builder at the top of the page, but it seems so verbose for such a simple issue. How can I tell C# "I'm done with this @ code section?"

Edit: a suggestion said my question was like this one, so here's how it's different: I am using the solution to that question in my code example, and still getting an error.

like image 251
Travis Heeter Avatar asked Jun 18 '26 21:06

Travis Heeter


1 Answers

try parenthesis ( )

<img src="/img/car-@(Model.Number).jpg" />

Take a look here C# Razor Syntax Quick Reference

like image 173
Christian Phillips Avatar answered Jun 21 '26 11:06

Christian Phillips



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!