Akhtubir
Akhtubir

Reputation: 107

set src property of img dynamically

instead of

src="~/Images/MyPage/goals.jpg" 

I would like something like this:

src="'~/Images/MyPage/' + '@MyMethod(1)'"

where

 public static string MyMethod(int someGivenId)
 {
    switch(someGivenId)
    {

      case 1: return "goals.jpg";    
      case 2: return "goals2.jpg";
      case 3: return "goals3.jpg";    
      default:
              return "goals3.jpg";
   }
}

How can I achieve this?

Upvotes: 1

Views: 33

Answers (1)

Pete
Pete

Reputation: 58422

If you have the relevant using statement at the top of your cshtml file (or in the view web.config), you should just be able to call the method directly:

<img src="~/Images/MyPage/@MyMethod(1)">

Upvotes: 1

Related Questions