0

I need to load an image that is named by the current user's username. This is stored in a session variable. The photo of the user must appear on the page by loading depending on the variable name. For example, if the name of the logged in user is "john", john.jpg must be loaded. So far, I have tried:

<img src="../Content/img/@Session["username"].jpg" class="user-image" alt="User Image" />

but this gives an error saying there is no object or definition name for jpg, probably because it takes the .jpg suffix as a method. Any idea on how to resolve this? Thanks in advance.

1
  • 1
    Try like this <img src="../Content/img/" +@Session["username"] + ".jpg" class="user-image" alt="User Image" /> Commented Jul 17, 2015 at 10:27

1 Answer 1

1
@{
    var username = Session["username"];
}
<img src="../Content/img/@(username).jpg" class="user-image" alt="User Image" />

OR

<img src="../Content/img/@(Session["username"]).jpg" class="user-image" alt="User Image" />

Example: http://razorpad.apphb.com/67AJoH5

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.