Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to have @url.Action on an <img> tag

I have a tag and I'm using Razor syntax. Im using the @url.Action html helper to pass a parameter to a method on a controller to retrieve the picture. But I want to have this picture as a link so when the user click the picture it would go to the controller and open a different view. So I tried the both ways show below but it's telling me that it's missing a "}" for "Tryone". And for "Trytwo" its not giving me any errors but it does not show me the picture as a link. Any ideas of the wright way to do this?

tryone

@foreach (var p in Model)
{   
     <a href= "@Url.Action("Index","Home")>           
    <img width="50" height="50"
          src= "@Url.Action("GetImage", "Sells", new {p.ItemID })" />
     </a>                                        
}

trytwo

@foreach (var p in Model)
{                    
    <img href="@Url.Action("Index","Home")" width="50" height="50"
          src= "@Url.Action("GetImage", "Sells", new {p.ItemID })" />                                                 
}
like image 216
CodeEngine Avatar asked Dec 25 '22 18:12

CodeEngine


2 Answers

An issue with your first attempt is that the href attribute is missing a closing quotation mark before the ending angle brace.

@foreach (var p in Model)
{   
     <a href= "@Url.Action("Index","Home")">           
    <img width="50" height="50"
          src= "@Url.Action("GetImage", "Sells", new {p.ItemID })" />
     </a>                                        
}
like image 72
sjager Avatar answered Dec 31 '22 14:12

sjager


trytwo is not working because img does not support href attribute.

Use your first approach with correct syntax - add quote (") at the end of href value.

like image 38
rageit Avatar answered Dec 31 '22 15:12

rageit