kevin
kevin

Reputation: 2213

How to ecape the "." character in MVC Razor

I have a MVC view where I need to render something like this:

<img src="foo/@(Model.Id).jpg" />

The problem is that Razor thinks the dot in ".jpg" belongs to the previous statement and thus tells me that there are no such field or property called "jpg". How can I overcome this?

Upvotes: 0

Views: 68

Answers (2)

Kirk
Kirk

Reputation: 5077

Try this version, this would probably work

Edited:

<img src=@("foo/" + Model.Id  + ".jpg") /> <!-- Try this simplified version -->

Upvotes: 2

Sergey Litvinov
Sergey Litvinov

Reputation: 7458

Your example works fine for me and display correct html with ID in it foo/51.jpg. 51 is number from my model.

But just as another idea you can save it as local variable like this:

@{
    string path = "foo/" + Model.Id + ".jpg";
}
<img src="@path" />

Upvotes: 1

Related Questions