Ruchir Gupta
Ruchir Gupta

Reputation: 1000

Razor Syntax in MVC with VB

In my view, there is a Model that holds a list of User objects. I save the profile pictures of these users in ~/thumbs folder as <username>.png. Now, I want to display all users with their profile pictures and name. So I've wrote the code as shown below

@Code
    For Each item in Model
        @<div class="col-md-2">
            <img class="img-responsive" src="~/thumbs/@item.username.png" alt="">
        </div>
    Next
End Code

Here, @item.username.png generates an error because the intellisense of Visual Studio assumes that I want to display the png property of the username of the @item

If I put a space before .png and write the src attribute as ~/thumbs/@item.username .png the error gets resolved, but when I run the page; it doesn't show the images since ultimately the src is converted to thumbs/<username>%20.png

What can I do? Please advise!

Upvotes: 1

Views: 56

Answers (2)

StuartLC
StuartLC

Reputation: 107267

As an alternative, as per Daniel's comment, you'll can also pre-format the string, or build the string inline, e.g. with String.Format:

<img class="img-responsive" src="@String.Format("~/thumbs/{0}.png", item.username)" alt="">

Upvotes: 1

Vishal Sharma
Vishal Sharma

Reputation: 2803

use following to avoid compile razor syntax problem

<img class="img-responsive" src="~/thumbs/@(item.username).png" alt="">

refer here quick - reference

Upvotes: 2

Related Questions